给我写一个在revit中C#写的cad文件链接到revit项目当前视图中的代码使用revit2016的api
以下是链接CAD文件到Revit项目当前视图中的C#代码:
-
首先,需要添加Revit API的引用,在Visual Studio中选择项目,右键单击“引用”,然后选择“添加引用” 。在“浏览”选项卡下,找到“Autodesk.Revit.dll”和“Autodesk.Revit.DB.dll”文件并添加它们作为引用。
-
创建一个新的C#文件,并添加以下命名空间:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Autodesk.Revit.DB; using Autodesk.Revit.UI; using Autodesk.Revit.UI.Selection;
- 在该文件中,创建一个公共类,并将其命名为“LinkCAD”:
public class LinkCAD { //添加代码 }
- 在LinkCAD类中,添加一个公共方法,并将其命名为“AddCADLink”:
public static void AddCADLink(Document doc) { //添加代码 }
- 在AddCADLink方法中,获取当前视图对象:
View view = doc.ActiveView;
- 使用文件选择器选择CAD文件:
System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog(); dlg.Filter = "CAD Files (.dwg)|.dwg"; dlg.Title = "Select CAD File"; if (dlg.ShowDialog() != System.Windows.Forms.DialogResult.OK) { return; }
- 获取CAD文件的路径:
string cadFilePath = dlg.FileName;
- 获取CAD文件的名称:
string cadFileName = System.IO.Path.GetFileNameWithoutExtension(cadFilePath);
- 创建一个CAD链接对象:
CADLinkType cadLinkType = CADLinkType.Create(doc, cadFilePath, new CADLinkOptions(true));
- 将CAD链接对象添加到视图中:
ElementId cadLinkTypeId = cadLinkType.GetTypeId();
FilteredElementCollector collector = new FilteredElementCollector(doc, view.Id);
IList
- 最后,将CAD链接对象的位置设置为视图的中心:
BoundingBoxXYZ boundingBox = importInstance.get_BoundingBox(null); double centerX = (boundingBox.Max.X + boundingBox.Min.X) / 2; double centerY = (boundingBox.Max.Y + boundingBox.Min.Y) / 2; XYZ center = new XYZ(centerX, centerY, 0); view.SetOrientation(new ViewOrientation3D(center, new XYZ(0, 0, 1), new XYZ(0, 1, 0)));
现在,您可以在Revit中调用AddCADLink方法来将CAD文件链接到当前视图中。请注意,此方法需要一个Document对象作为参数。
原文地址: http://www.cveoy.top/t/topic/4ne 著作权归作者所有。请勿转载和采集!