关于Revit Journal读写的例子。

 

#region Namespaces
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
#endregion namespace Journaling
{
[Autodesk.Revit.Attributes.Transaction(TransactionMode.Manual)]
[Autodesk.Revit.Attributes.Regeneration(RegenerationOption.Manual)]
[Autodesk.Revit.Attributes.Journaling(JournalingMode.NoCommandData)]
public class Command : IExternalCommand
{
public Result Execute(
ExternalCommandData commandData,
ref string message,
ElementSet elements)
{
try
{
Transaction tran = new Transaction(commandData.Application.ActiveUIDocument.Document, "Journaling");
tran.Start();
Journaling deal = new Journaling(commandData);
deal.Run();
tran.Commit(); return Result.Succeeded;
}
catch (Exception ex)
{
message = ex.Message;
return Result.Failed;
} }
}
}

 

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Creation = Autodesk.Revit.Creation; namespace Journaling
{
public class Journaling
{
class WallTypeComparer : IComparer<WallType>
{
int IComparer<WallType>.Compare(WallType first, WallType second)
{
return string.Compare(first.Name, second.Name);
}
} ExternalCommandData m_commandData;
bool m_canReadData; XYZ m_startPoint;
XYZ m_endPoint;
Level m_createlevel;
WallType m_createType; List<Level> m_levelList;
List<WallType> m_wallTypeList; public ReadOnlyCollection<Level> Levels
{
get
{
return new ReadOnlyCollection<Level>(m_levelList);
}
} public ReadOnlyCollection<WallType> WallTypes
{
get
{
return new ReadOnlyCollection<WallType>(m_wallTypeList);
}
} public Journaling(ExternalCommandData commandData)
{
m_commandData = commandData;
m_canReadData = (0 < commandData.JournalData.Count) ? true : false; m_levelList = new List<Level>();
m_wallTypeList = new List<WallType>();
InitializeListData();
} public void Run()
{
if (m_canReadData)
{
ReadJournalData();
CreateWall();
}
else
{
if (!DisplayUI())
{
return;
} CreateWall();
WriteJournalData();
}
} public void SetNecessaryData(XYZ startPoint, XYZ endPoint, Level level, WallType type)
{
m_startPoint = startPoint;
m_endPoint = endPoint;
m_createlevel = level;
m_createType = type; } private void InitializeListData()
{
if (null == m_wallTypeList || null == m_levelList)
{
throw new Exception("necessary data members don't initialize.");
} Document document = m_commandData.Application.ActiveUIDocument.Document;
FilteredElementCollector filteredElementCollector = new FilteredElementCollector(document);
filteredElementCollector.OfClass(typeof(WallType));
m_wallTypeList = filteredElementCollector.Cast<WallType>().ToList<WallType>(); WallTypeComparer comparer = new WallTypeComparer();
m_wallTypeList.Sort(comparer); FilteredElementIterator iter = (new FilteredElementCollector(document)).OfClass(typeof(Level)).GetElementIterator();
iter.Reset();
while (iter.MoveNext())
{
Level level = iter.Current as Level;
if (null == level)
{
continue;
}
m_levelList.Add(level);
} } private void ReadJournalData()
{
Document doc = m_commandData.Application.ActiveUIDocument.Document;
IDictionary<string, string> dataMap = m_commandData.JournalData;
string dataValue = null;
dataValue = GetSpecialData(dataMap, "Wall Type Name");
foreach (WallType type in m_wallTypeList)
{
if (dataValue == type.Name)
{
m_createType = type;
break;
}
}
if (null == m_createType)
{
throw new InvalidDataException("Can't find the wall type from the journal.");
} dataValue = GetSpecialData(dataMap, "Level Id");
ElementId id = new ElementId(Convert.ToInt32(dataValue)); m_createlevel = doc.GetElement(id) as Level;
if (null == m_createlevel)
{
throw new InvalidDataException("Can't find the level from the journal.");
} dataValue = GetSpecialData(dataMap, "Start Point");
m_startPoint = StringToXYZ(dataValue); if (m_startPoint.Equals(m_endPoint))
{
throw new InvalidDataException("Start point is equal to end point.");
}
} private bool DisplayUI()
{
using (JournalingForm displayForm = new JournalingForm(this))
{
displayForm.ShowDialog();
if (DialogResult.OK != displayForm.DialogResult)
{
return false;
}
}
return true;
} private void CreateWall()
{
Creation.Application createApp = m_commandData.Application.Application.Create;
Creation.Document createDoc = m_commandData.Application.ActiveUIDocument.Document.Create; Line geometryLine = Line.CreateBound(m_startPoint, m_endPoint);
if (null == geometryLine)
{
throw new Exception("Create the geometry line failed.");
} Wall createdWall = Wall.Create(m_commandData.Application.ActiveUIDocument.Document,
geometryLine, m_createType.Id, m_createlevel.Id,
15, m_startPoint.Z + m_createlevel.Elevation, true, true);
if (null == createdWall)
{
throw new Exception("Create the wall failed.");
} } private void WriteJournalData()
{
IDictionary<string, string> dataMap = m_commandData.JournalData;
dataMap.Clear(); dataMap.Add("Wall Type Name", m_createType.Name);
dataMap.Add("Level Id", m_createlevel.Id.IntegerValue.ToString());
dataMap.Add("Start Point", XYZToString(m_startPoint));
dataMap.Add("End Point", XYZToString(m_endPoint));
} private static XYZ StringToXYZ(string pointString)
{
double x = 0;
double y = 0;
double z = 0;
string subString; subString = pointString.TrimStart('(');
subString = subString.TrimEnd(')');
string[] coordinateString = subString.Split(',');
if (3 != coordinateString.Length)
{
throw new InvalidDataException("The point iniformation in journal is incorrect");
} try
{
x = Convert.ToDouble(coordinateString[0]);
y = Convert.ToDouble(coordinateString[1]);
z = Convert.ToDouble(coordinateString[2]);
}
catch (Exception)
{
throw new InvalidDataException("The point information in journal is incorrect"); } return new Autodesk.Revit.DB.XYZ(x, y, z);
} private static string XYZToString(XYZ point)
{
string pointString = "(" + point.X.ToString() + "," + point.Y.ToString() + ","
+ point.Z.ToString() + ")";
return pointString;
} private static string GetSpecialData(IDictionary<string, string> dataMap, string key)
{
string dataValue = dataMap[key]; if (string.IsNullOrEmpty(dataValue))
{
throw new Exception(key + "information is not exits in journal.");
}
return dataValue;
} }
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; using Autodesk.Revit.DB;
using Autodesk.Revit.UI; namespace Journaling
{
public partial class JournalingForm : System.Windows.Forms.Form
{
const double Precision = 0.00001;
Journaling m_dataBuffer; public JournalingForm(Journaling dataBuffer)
{
InitializeComponent(); m_dataBuffer = dataBuffer; typeComboBox.DataSource = m_dataBuffer.WallTypes;
typeComboBox.DisplayMember = "Name";
levelComboBox.DataSource = m_dataBuffer.Levels;
levelComboBox.DisplayMember = "Name";
} private void okButton_Click(object sender, EventArgs e)
{ XYZ startPoint = new XYZ(Convert.ToDouble(textBox1.Text.Trim()),
Convert.ToDouble(textBox2.Text.Trim()),
Convert.ToDouble(textBox3.Text.Trim())); XYZ endPoint = new XYZ(Convert.ToDouble(textBox4.Text.Trim()),
Convert.ToDouble(textBox5.Text.Trim()),
Convert.ToDouble(textBox6.Text.Trim())); if (startPoint.Equals(endPoint))
{
TaskDialog.Show("Revit", "Start point should not equal end point.");
return;
} double diff = Math.Abs(startPoint.Z - endPoint.Z);
if (diff > Precision)
{
TaskDialog.Show("Revit", "Z coordinate of start and end points should be equal.");
return;
} Level level = levelComboBox.SelectedItem as Level; // level information
if (null == level) // assert it isn't null
{
TaskDialog.Show("Revit", "The selected level is null or incorrect.");
return;
} WallType type = typeComboBox.SelectedItem as WallType; // wall type
if (null == type) // assert it isn't null
{
TaskDialog.Show("Revit", "The selected wall type is null or incorrect.");
return;
} // Invoke SetNecessaryData method to set the collected support data
m_dataBuffer.SetNecessaryData(startPoint, endPoint, level, type); // Set result information and close the form
this.DialogResult = DialogResult.OK;
this.Close();
} private void cancelButton_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
} }
}

Revit二次开发示例:Journaling的更多相关文章

  1. Revit二次开发示例:HelloRevit

    本示例实现Revit和Revit打开的文件的相关信息. #region Namespaces using System; using System.Collections.Generic; using ...

  2. Revit二次开发示例:EventsMonitor

    在该示例中,插件在Revit启动时弹出事件监控选择界面,供用户设置,也可在添加的Ribbon界面完成设置.当Revit进行相应操作时,弹出窗体会记录事件时间和名称. #region Namespace ...

  3. Revit二次开发示例:ErrorHandling

    本示例介绍了Revit的错误处理.   #region Namespaces using System; using System.Collections.Generic; using Autodes ...

  4. Revit二次开发示例:ChangesMonitor

    在本示例中,程序监控Revit打开文件事件,并在创建的窗体中更新文件信息.   #region Namespaces using System; using System.Collections.Ge ...

  5. Revit二次开发示例:AutoStamp

    该示例中,在Revit启动时添加打印事件,在打印时向模型添加水印,打印完成后删除该水印.   #region Namespaces using System; using System.Collect ...

  6. Revit二次开发示例:ModelessForm_ExternalEvent

    使用Idling事件处理插件任务. #region Namespaces using System; using System.Collections.Generic; using Autodesk. ...

  7. Revit二次开发示例:DisableCommand

    Revit API 不支持调用Revit内部命令,但可以用RevitCommandId重写它们(包含任意选项卡,菜单和右键命令).使用RevitCommandId.LookupCommandId()可 ...

  8. Revit二次开发示例:DesignOptions

    本例只要演示Revit的类过滤器的用法,在对话框中显示DesignOption元素. #region Namespaces using System; using System.Collections ...

  9. Revit二次开发示例:DeleteObject

    在本例中,通过命令可以删除选中的元素. 需要注意的是要在代码中加入Transaction,否则的话会出现Modifying  is forbidden because the document has ...

随机推荐

  1. Unity3D研究院之自制批量关联材质与贴图插件

    原地址:http://www.xuanyusong.com/archives/2314 美术做过的模型导出fbx,美术把Fbx和贴图文件给了程序,程序把Fbx导入工程可能会出现贴图和材质没有关联上的问 ...

  2. search in 2d matrix and serach minimum in rotated array

    import java.io.*; import java.lang.reflect.Array; import java.util.Arrays; import java.util.Collecti ...

  3. Linux常用热键(持续更新)

    (这些文章都是从我的个人主页上粘贴过来的,大家也可以访问我的主页 www.iwangzheng.com) --圣诞节怎么过, --略过. 今天装ubuntu的时候把windows覆盖了, 凌乱,TX童 ...

  4. Opencv Cookbook阅读笔记(四):用直方图统计像素

    灰度直方图的定义 灰度直方图是灰度级的函数,描述图像中该灰度级的像素个数(或该灰度级像素出现的频率):其横坐标是灰度级,纵坐标表示图像中该灰度级出现的个数(频率). #include <open ...

  5. ZeroMQ(java)之负载均衡

    我们在实际的应用中最常遇到的场景如下: A向B发送请求,B向A返回结果.... 但是这种场景就会很容易变成这个样子: 很多A向B发送请求,所以B要不断的处理这些请求,所以就会很容易想到对B进行扩展,由 ...

  6. Step

    php+MySQL html+css JQuery Mobile JavaScript weiPHP Sina Cloud 微信公众订阅号平台开发

  7. 在Shell里面判断字符串是否为空

     在Shell里面判断字符串是否为空 分类: Linux shell2011-12-28 23:18 15371人阅读 评论(0) 收藏 举报 shell 主要有以下几种方法: echo “$str” ...

  8. raw格式镜像文件压缩并转换为qcow2格式

    raw格式文件,这个比较占用空间,你可以用以下命令将其压缩并转换成qcow2格式. # virt-sparsify --compress --convert qcow2 ubuntu.img ubun ...

  9. 【OpenStack】OpenStack系列3之Swift详解

    Swift安装部署(与keystone依赖包有冲突,需要安装不同版本eventlet) 参考:http://www.server110.com/openstack/201402/6662.html h ...

  10. 多个list合并

    需要多个list合并,如图 @SuppressWarnings("unchecked")    @Override    public List<CwInfo> get ...