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 ...
随机推荐
- SGU-169 Numbers(找规律)
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=169 解题报告: P(n)定义为n的所有位数的乘积,例如P(1243)=1*2*3* ...
- apache virtualhost配置 apache配置多个网站
第一步 apache下httpd.conf文件 启用模块LoadModule vhost_alias_module modules/mod_vhost_alias.so 第二步 apache下http ...
- linux下搭建svn版本控制软件
svn作为曾经流行的版本控制控制软件,它优异的版本控制功能在有意无意间已经深入IT人的工作了.然而虽然有后起之秀的git,但现在使用svn的项目并不在少数.最近有个项目需要协同开发,由于对svn用得比 ...
- sqlite 使用记录
2014年8月13日 18:20:52 SQLite中创建自增字段: 简单的回答:一个声明为 INTEGER PRIMARY KEY 的字段将自动增加. 从 SQLite 的 2.3.4 版本开始,如 ...
- href脱离iframe显示
iframe框架页面如下: <!DOCTYPE html><html lang="zh"><head><meta name='viewpo ...
- NEFU 2016省赛演练一 B题(递推)
HK Problem:B Time Limit:2000ms Memory Limit:65535K Description yy is interested in numbers and yy nu ...
- eclipse原文件编码GBK-UTF8
菜单Window-Preferences-General-Workspace,在右侧面板的Text file encoding选项中选择Other:UTF-8 上一步如果不起作用,可以尝试设置下: W ...
- 高效开发 Android App 的 10 个建议(转)
文章写的非常好,值得大家好好研究研究,仔细分析一下. 引文地址: http://www.cnblogs.com/xiaochao1234/p/3644989.html 假如要Google Play上做 ...
- Hadoop学习:
文件名称 格式 描述 hadoop-env.sh BaSh 脚本 记 录 脚 本 要 用 的 环 境 变 , 以 运 行Hadoop core-site.xml HadooP配XML Hadoop C ...
- Android textView点击滚动(跑马灯)效果
布局文件: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:to ...