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 ...
随机推荐
- centos配置yum源
1.登录mirrors.163.com 2.点击centos后面的“centos使用帮助” 3.下载CentOS7-Base-163.repo 4.sudo mv /etc/yum.repos.d/C ...
- L18 如何快速查找文档获得帮助
原地址:http://www.howzhi.com/course/286/lesson/2121 查找文档快速 苹果提供了丰富的文档,以帮助您成功构建和部署你的应用程序,包括示例代码,常见问题解答,技 ...
- twisted udp编程
概述 Unlike TCP, UDP has no notion of connections. A UDP socket can receive datagrams from any server ...
- Android mtk单路录音问题
在单路录音中,有两种情况导致底层录音资源被占用的问题: 1 开启vmLog后,拨打一个电话,挂断电话.如果挂断电话后,没有关闭vmlog进程,则会导致其它AP 无法得到底层的录音资源,从而无法录音. ...
- 【OpenStack】OpenStack系列17之OpenStack私有云设计一
[软件系统] 1.操作系统(Minimal最小化安装): CentOS-6.6-x86_64,CentOS 6最后一个版本,官方建议版本. 相对于6.5版本: 强化对 SCSI 设备的处理,有助应付某 ...
- 《ASP.NET1200例》解决母版页报错“内容控件必须是内容页中的顶级控件,或是引用母版页的嵌套母版页。”
VS2005下,添加了母版页这个控件,我们可以讲N个页面中共同的部分放在母版页来实现,并让WEB窗体集成自我们的母版页,就可以让我们的站点具有统一的风格了.在VS2005SP1之前的版本中,我们只能创 ...
- #ifdef 的技巧用法
-- int _tmain(int argc, _TCHAR* argv[]) { #ifdef DEBUG cout<<"DEBUG has been defined" ...
- Python多进程(1)——subprocess与Popen()
Python多进程方面涉及的模块主要包括: subprocess:可以在当前程序中执行其他程序或命令: mmap:提供一种基于内存的进程间通信机制: multiprocessing:提供支持多处理器技 ...
- Python -- BeautifulSoup的学习使用
BeautifulSoup4.3 的使用 下载和安装 # 下载 http://www.crummy.com/software/BeautifulSoup/bs4/download/ # 解压后 使用r ...
- cenos配置
#修复ifconfig1.查看 /sbin/ifconfig是否存在 echo $PATH2.查看ifconfig命令是否存在ls /sbin |grep ifconfig如果ifconfig命令存在 ...