Revit二次开发示例:Journaling
关于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的更多相关文章
- Revit二次开发示例:HelloRevit
本示例实现Revit和Revit打开的文件的相关信息. #region Namespaces using System; using System.Collections.Generic; using ...
- Revit二次开发示例:EventsMonitor
在该示例中,插件在Revit启动时弹出事件监控选择界面,供用户设置,也可在添加的Ribbon界面完成设置.当Revit进行相应操作时,弹出窗体会记录事件时间和名称. #region Namespace ...
- Revit二次开发示例:ErrorHandling
本示例介绍了Revit的错误处理. #region Namespaces using System; using System.Collections.Generic; using Autodes ...
- Revit二次开发示例:ChangesMonitor
在本示例中,程序监控Revit打开文件事件,并在创建的窗体中更新文件信息. #region Namespaces using System; using System.Collections.Ge ...
- Revit二次开发示例:AutoStamp
该示例中,在Revit启动时添加打印事件,在打印时向模型添加水印,打印完成后删除该水印. #region Namespaces using System; using System.Collect ...
- Revit二次开发示例:ModelessForm_ExternalEvent
使用Idling事件处理插件任务. #region Namespaces using System; using System.Collections.Generic; using Autodesk. ...
- Revit二次开发示例:DisableCommand
Revit API 不支持调用Revit内部命令,但可以用RevitCommandId重写它们(包含任意选项卡,菜单和右键命令).使用RevitCommandId.LookupCommandId()可 ...
- Revit二次开发示例:DesignOptions
本例只要演示Revit的类过滤器的用法,在对话框中显示DesignOption元素. #region Namespaces using System; using System.Collections ...
- Revit二次开发示例:DeleteObject
在本例中,通过命令可以删除选中的元素. 需要注意的是要在代码中加入Transaction,否则的话会出现Modifying is forbidden because the document has ...
随机推荐
- ListView异步加载网络图片完美版之双缓存技术
本示例参考学习了一个国外的示例:http://code.google.com/p/android-imagedownloader/,有兴趣的同学下载研究一下. 问题描述:在这一篇博客中将会为大家讲解如 ...
- BeautifulSoup获取指定class样式的div
如何获取指定的标签的内容是解析网页爬取数据的必要手段,比如想获取<div class='xxx'> ...<div>这样的div标签,通常有三种办法, 1)用字符串查找方法,然 ...
- LVM XFS增加硬盘分区容量(resize2fs: Bad magic number in super-block while)
LVM XFS增加硬盘分区容量(resize2fs: Bad magic number -- :: 分类: Linux LVM XFS增加硬盘分区容量(resize2fs: Bad magic num ...
- TCP的几个状态 (SYN, FIN, ACK, PSH, RST, URG)
在TCP层,有个FLAGS字段,这个字段有以下几个标识:SYN, FIN, ACK, PSH, RST, URG. 其中,对于我们日常的分析有用的就是前面的五个字段. 它们的含义是: SYN表示建立连 ...
- HTTP认证相关
Java HTTPBasicAuth http://blog.csdn.net/kkdelta/article/details/28419625Python HTTPBasicAuth http:// ...
- Android Volley获取json格式的数据
为了让Android能够快速地访问网络和解析通用的数据格式Google专门推出了Volley库,用于Android系统的网络传输.volley库可以方便地获取远程服务器的图片.字符串.json对象和j ...
- codeforces 478B Random Teams 解题报告
题目链接:http://codeforces.com/problemset/problem/478/B 题目意思:有 n 个人,需要将这班人分成 m 个 组,每个组至少含有一个人,同一个组里的人两两可 ...
- [] ubuntu 14.04 搜狗拼音输入法安装
1.需要现在ubuntu下安装fcitx和libssh2-1命令如下 sudo apt-get install fcitx sudo apt-get install libssh2-1 注意最好是先卸 ...
- php 指针遍历、预定义数组和常用函数
<?php /*//定义 $attr = array(1,2,3); $attr[] = 1; $attr = array("one"=>"hello&quo ...
- stsadm.exe
Usage: stsadm.exe -o <operation> [<parameters>] stsadm.exe -help [<operation>] Ope ...