Revit二次开发--创建图纸并添加视图

创建图纸并向图纸中添加视图,主要用到了两个静态方法:
public static ViewSheet Create(Document document, ElementId titleBlockTypeId)//创建图纸
public static Viewport Create(Document document, ElementId viewSheetId, ElementId viewId, XYZ point)
//在图纸的给定位置添加视图

 public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
    {
        UIDocument uiDoc = commandData.Application.ActiveUIDocument;
        Document doc = uiDoc.Document;

        FilteredElementCollector collector = new FilteredElementCollector(doc);
        collector.OfCategory(BuiltInCategory.OST_TitleBlocks).OfClass(typeof(FamilySymbol));
        FamilySymbol fs = collector.FirstElement() as FamilySymbol;
        
        //创建图纸
        ViewSheet viewSheet = null;
        if (fs != null)
        {
            using (Transaction trans = new Transaction(doc))
            {
                trans.Start("Create a new Viewsheet");
                try
                {
                    viewSheet = ViewSheet.Create(doc,fs.Id);
                    if (null == viewSheet)
                    {
                        throw new Exception("failed to create new ViewSheet.");
                    }
                    trans.Commit();
                }
                catch (Exception )
                {
                    trans.RollBack();
                }
            }
        }

        //向图纸中添加视图
        FilteredElementCollector collectoView = new FilteredElementCollector(doc);
        collectoView.OfClass(typeof(View)).OfCategory(BuiltInCategory.OST_Views);
        View view = collectoView.FirstElement() as View;
        
        UV location = new UV((viewSheet.Outline.Max.U - viewSheet.Outline.Min.U) / 2, (viewSheet.Outline.Max.V - viewSheet.Outline.Min.V) / 2);
        using (Transaction trans = new Transaction(doc))
        {
            trans.Start("create viewPort");
            
            try
            {
                Viewport.Create(doc, viewSheet.Id, view.Id, new XYZ(location.U, location.V, 0));
                trans.Commit();
            }
            catch 
            {
                trans.RollBack();
            }                
        }
        return Result.Succeeded;
    }
Revit二次开发中,可以使用Revit API来创建图纸、图例和明细表。以下是一些示例代码以供参考: 1. 创建图纸 ```csharp // 获取当前文档 Document doc = commandData.Application.ActiveUIDocument.Document; // 创建新的图纸视图 View newView = ViewDrafting.Create(doc, new XYZ(0, 0, 0)); // 设置图纸视图的名称和编号 newView.Name = "New Sheet"; newView.SheetNumber = "A101"; // 获取所有的图纸集合 ViewSheetSet sheetSet = doc.GetElement(doc.GetDefaultElementTypeId(ElementTypeGroup.ViewSheet)) as ViewSheetSet; // 创建新的图纸 ViewSheet newSheet = ViewSheet.Create(doc, sheetSet.ViewsTemplate); newSheet.Name = "New Sheet"; newSheet.SheetNumber = "A101"; // 将图纸视图添加图纸上 newSheet.Views.Add(newView.Id); // 显示图纸 doc.Regenerate(); ``` 2. 创建图例 ```csharp // 获取当前文档 Document doc = commandData.Application.ActiveUIDocument.Document; // 创建新的图例视图 View newView = ViewLegend.Create(doc, new XYZ(0, 0, 0)); // 设置图例视图的名称 newView.Name = "New Legend"; // 将图例视图添加到项目视图集合中 doc.ProjectInformation.Legends.Add(newView.Id); // 显示图例 doc.Regenerate(); ``` 3. 创建明细表 ```csharp // 获取当前文档 Document doc = commandData.Application.ActiveUIDocument.Document; // 获取所有的视图 FilteredElementCollector viewCollector = new FilteredElementCollector(doc).OfClass(typeof(View)); // 创建新的明细表视图 ViewSchedule newView = ViewSchedule.CreateSchedule(doc, viewCollector.First().Id); // 设置明细表视图的名称和类别 newView.Name = "New Schedule"; newView.Definition.Category = BuiltInCategory.OST_Doors; // 显示明细表 doc.Regenerate(); ``` 以上代码仅供参考,具体实现可能会因需求而异。在实际开发中,需要根据具体需求来调整代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值