public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { UIApplication uiapp = commandData.Application; UIDocument uidoc = uiapp.ActiveUIDocument; Application app = uiapp.Application; Document doc = uidoc.Document; IList <double> wallthicknessList = new List <double>(); XYZ normal = new XYZ(0, 0, 1); string previous = null; double previousWidth = 0; SpatialElementBoundaryOptions seb_opt = new SpatialElementBoundaryOptions(); FilteredElementCollector levels = new FilteredElementCollector(doc).OfClass(typeof(Level)); using (Transaction tx = new Transaction(doc)) { tx.Start("Create cropped views for each room"); string date_iso = DateTime.Now.ToString("yyyy-MM-dd"); foreach (Level level in levels) { Debug.Print(level.Name); ElementId id_view = level.FindAssociatedPlanViewId(); ViewPlan view = doc.GetElement(id_view) as ViewPlan; IEnumerable <Room> rooms = new FilteredElementCollector(doc, id_view).OfClass(typeof(SpatialElement)).Where <Element>(e => e is Room).Cast <Room>(); foreach (Room room in rooms) { wallthicknessList.Clear(); string view_name = string.Format("{0}_cropped_to_room_{1}_date_{2}", view.Name, room.Name, date_iso); id_view = view.Duplicate(ViewDuplicateOption.AsDependent); View view_cropped = doc.GetElement(id_view) as View; view_cropped.Name = view_name; IList <IList <BoundarySegment> > sloops = room.GetBoundarySegments(seb_opt); if (null == sloops) // the room may not be bound { continue; } CurveLoop loop = null; foreach (IList <BoundarySegment> sloop in sloops) { loop = new CurveLoop(); foreach (BoundarySegment s in sloop) { loop.Append(s.GetCurve()); ElementType type = doc.GetElement(s.ElementId) as ElementType; Element elem = doc.GetElement(s.ElementId); //Error when wall width varies across length //if (elem is Wall) //{ // Wall wall = elem as Wall; // wallthicknessList.Add(wall.Width*1.1); //} //else //{ // //Room separator // //Any other exceptions to walls need including?? // wallthicknessList.Add(0); //} if (elem != null)//The elem == null is due to room separators. Are there going to be any others? { if (firstPass) { if ((BuiltInCategory)elem.Category.Id.IntegerValue == BuiltInCategory.OST_Walls) { Wall wall = elem as Wall; //Is there a better way of identifying Orientation (predominantly horizontal or Vertical) What if walls at 45 deg? if (Math.Abs(Math.Round(wall.Orientation.Y, 0)) == 1) { previous = "Y"; } else { previous = "X"; } firstPass = false; wallthicknessList.Add(wall.Width + 0.1); previousWidth = wall.Width + 0.1; } //Are there any other situations need taking account off? else if ((BuiltInCategory)elem.Category.Id.IntegerValue == BuiltInCategory.OST_RoomSeparationLines) { //Room separator Autodesk.Revit.DB.Options opt = new Options(); Autodesk.Revit.DB.GeometryElement geomElem = elem.get_Geometry(opt); foreach (GeometryObject geomObj in geomElem) { Line line = geomObj as Line; //Is there a better way of identifying Orientation (predominantly horizontal or Vertical) What if walls at 45 deg? if (0 - line.GetEndPoint(1).X - line.GetEndPoint(0).X > 0 - line.GetEndPoint(1).Y - line.GetEndPoint(0).Y) { previous = "Y"; } else { previous = "X"; } firstPass = false; wallthicknessList.Add(0.1); previousWidth = 0.1; } } } else { if ((BuiltInCategory)elem.Category.Id.IntegerValue == BuiltInCategory.OST_Walls) { Wall wall = elem as Wall; if (Math.Abs(Math.Round(wall.Orientation.Y, 0)) == 1 && previous == "Y") { if (wall.Width > previousWidth) { //This would appear to avoid the Cannot properly trim error //How to allow for more than two wall thickness changes within the same length? //How to not require this and just offset wall widths? wallthicknessList[wallthicknessList.Count - 1] = wall.Width + 0.1; } wallthicknessList.Add(wall.Width + 0.1); previousWidth = wall.Width + 0.1; previous = "Y"; } else if (Math.Abs(Math.Round(wall.Orientation.X, 0)) == 1 && previous == "X") { if (wall.Width > previousWidth) { //This would appear to avoid the Cannot properly trim error //How to allow for more than two wall thickness changes within the same length? //How to not require this and just offset wall widths? wallthicknessList[wallthicknessList.Count - 1] = wall.Width + 0.1; } wallthicknessList.Add(wall.Width + 0.1); previousWidth = wall.Width + 0.1; previous = "X"; } else { if (Math.Abs(Math.Round(wall.Orientation.Y, 0)) == 1) { previous = "Y"; } else if (Math.Abs(Math.Round(wall.Orientation.X, 0)) == 1) { previous = "X"; } wallthicknessList.Add(wall.Width + 0.1); previousWidth = wall.Width + 0.1; } } else if ((BuiltInCategory)elem.Category.Id.IntegerValue == BuiltInCategory.OST_RoomSeparationLines)//Any other situations need taking account of? { //Room separator Autodesk.Revit.DB.Options opt = new Options(); Autodesk.Revit.DB.GeometryElement geomElem = elem.get_Geometry(opt); foreach (GeometryObject geomObj in geomElem) { Line line = geomObj as Line; //Is there a better way of identifying Orientation (predominantly horizontal or Vertical) What if walls at 45 deg? if (0 - line.GetEndPoint(1).X - line.GetEndPoint(0).X > 0 - line.GetEndPoint(1).Y - line.GetEndPoint(0).Y) { previous = "Y"; } else { previous = "X"; } wallthicknessList.Add(previousWidth); //Error "cannot trim" //wallthicknessList.Add(0.1); previousWidth = 0.1; } } } } else { wallthicknessList.Add(-0.1); } } // Skip out after first sloop - ignore // rooms with holes and disjunct parts break; } CurveLoop loop2 = CurveLoop.CreateViaOffset(loop, wallthicknessList, normal); CurveLoop newloop = new CurveLoop(); foreach (Curve curve in loop2) { List <XYZ> points = curve.Tessellate().ToList(); for (int ip = 0; ip < points.Count - 1; ip++) { Line l = Line.CreateBound(points[ip], points[ip + 1]); newloop.Append(l); } } ViewCropRegionShapeManager vcrs_mgr = view_cropped.GetCropRegionShapeManager(); bool valid = vcrs_mgr.IsCropRegionShapeValid(newloop); if (valid) { view_cropped.CropBoxVisible = true; view_cropped.CropBoxActive = true; vcrs_mgr.SetCropShape(newloop); } } } tx.Commit(); } return(Result.Succeeded); }
public Result Execute( ExternalCommandData commandData, ref string message, ElementSet elements) { UIApplication uiapp = commandData.Application; UIDocument uidoc = uiapp.ActiveUIDocument; Application app = uiapp.Application; Document doc = uidoc.Document; SpatialElementBoundaryOptions seb_opt = new SpatialElementBoundaryOptions(); FilteredElementCollector levels = new FilteredElementCollector(doc) .OfClass(typeof(Level)); using (Transaction tx = new Transaction(doc)) { tx.Start("Create cropped views for each room"); string date_iso = DateTime.Now.ToString("yyyy-MM-dd"); foreach (Level level in levels) { Debug.Print(level.Name); ElementId id_view = level.FindAssociatedPlanViewId(); ViewPlan view = doc.GetElement(id_view) as ViewPlan; IEnumerable <Room> rooms = new FilteredElementCollector(doc, id_view) .OfClass(typeof(SpatialElement)) .Where <Element>(e => e is Room) .Cast <Room>(); foreach (Room room in rooms) { string view_name = string.Format( "{0}_cropped_to_room_{1}_date_{2}", view.Name, room.Name, date_iso); id_view = view.Duplicate( ViewDuplicateOption.AsDependent); View view_cropped = doc.GetElement( id_view) as View; view_cropped.Name = view_name; IList <IList <BoundarySegment> > sloops = room.GetBoundarySegments(seb_opt); if (null == sloops) // the room may not be bounded { continue; } CurveLoop room_outer_loop = GetOuterLoopOfRoomFromCreateViaOffset( view_cropped, sloops); //ViewCropRegionShapeManager vcrs_mgr // = view_cropped.GetCropRegionShapeManager(); //bool valid = vcrs_mgr.IsCropRegionShapeValid( // room_outer_loop ); //if( valid ) //{ // view_cropped.CropBoxVisible = true; // view_cropped.CropBoxActive = true; // vcrs_mgr.SetCropShape( room_outer_loop ); //} } } tx.Commit(); } return(Result.Succeeded); }
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { UIApplication uiapp = commandData.Application; UIDocument uidoc = uiapp.ActiveUIDocument; Application app = uiapp.Application; Document doc = uidoc.Document; IList <double> wallthicknessList = new List <double>(); XYZ normal = new XYZ(0, 0, 1); SpatialElementBoundaryOptions seb_opt = new SpatialElementBoundaryOptions(); FilteredElementCollector levels = new FilteredElementCollector(doc).OfClass(typeof(Level)); using (Transaction tx = new Transaction(doc)) { tx.Start("Create cropped views for each room"); string date_iso = DateTime.Now.ToString("yyyy-MM-dd"); foreach (Level level in levels) { wallthicknessList.Clear(); Debug.Print(level.Name); ElementId id_view = level.FindAssociatedPlanViewId(); ViewPlan view = doc.GetElement(id_view) as ViewPlan; IEnumerable <Room> rooms = new FilteredElementCollector(doc, id_view).OfClass(typeof(SpatialElement)).Where <Element>(e => e is Room).Cast <Room>(); foreach (Room room in rooms) { wallthicknessList.Clear(); string view_name = string.Format("{0}_cropped_to_room_{1}_date_{2}", view.Name, room.Name, date_iso); id_view = view.Duplicate(ViewDuplicateOption.AsDependent); View view_cropped = doc.GetElement(id_view) as View; view_cropped.Name = view_name; IList <IList <BoundarySegment> > sloops = room.GetBoundarySegments(seb_opt); if (null == sloops) // the room may not be bound { continue; } CurveLoop loop = null; foreach (IList <BoundarySegment> sloop in sloops) { loop = new CurveLoop(); foreach (BoundarySegment s in sloop) { loop.Append(s.GetCurve()); ElementType type = doc.GetElement(s.ElementId) as ElementType; Element elem = doc.GetElement(s.ElementId); if (elem is Wall) { Wall wall = elem as Wall; wallthicknessList.Add(wall.Width); } else { //Room separator //Any other exceptions to walls need including?? wallthicknessList.Add(0); } } // Skip out after first sloop - ignore // rooms with holes and disjunct parts break; } CurveLoop loop2 = CurveLoop.CreateViaOffset(loop, wallthicknessList, normal); CurveLoop newloop = new CurveLoop(); foreach (Curve curve in loop2) { List <XYZ> points = curve.Tessellate().ToList(); for (int ip = 0; ip < points.Count - 1; ip++) { Line l = Line.CreateBound(points[ip], points[ip + 1]); newloop.Append(l); } } ViewCropRegionShapeManager vcrs_mgr = view_cropped.GetCropRegionShapeManager(); bool valid = vcrs_mgr.IsCropRegionShapeValid(newloop); if (valid) { view_cropped.CropBoxVisible = true; view_cropped.CropBoxActive = true; vcrs_mgr.SetCropShape(newloop); } } } tx.Commit(); } return(Result.Succeeded); }
public static List <ViewPlan> DuplicateViewPlan(this ViewPlan viewPlan, IEnumerable <Level> levels = null, bool useExisting = false) { if (viewPlan == null || !viewPlan.IsValidObject || viewPlan.IsTemplate) { return(null); } Document document = viewPlan.Document; if (document == null) { return(null); } List <Level> levels_Temp = null; if (levels != null) { levels_Temp = new List <Level>(levels); } if (levels_Temp == null) { levels_Temp = new FilteredElementCollector(document).OfClass(typeof(Level)).Cast <Level>().ToList(); } List <Element> elements_ColorFillLegend = new FilteredElementCollector(document, viewPlan.Id).OfCategory(BuiltInCategory.OST_ColorFillLegends).ToList(); List <ViewPlan> result = new List <ViewPlan>(); foreach (Level level in levels_Temp) { if (level.Id == viewPlan.GenLevel.Id) { continue; } ViewPlan viewPlan_New = null; List <ViewPlan> viewPlans = new FilteredElementCollector(document).OfClass(typeof(ViewPlan)).Cast <ViewPlan>().ToList(); string name = level.Name; if (useExisting) { viewPlan_New = viewPlans.Find(x => x.Name == name); } if (viewPlan_New == null) { viewPlan_New = ViewPlan.Create(document, viewPlan.GetTypeId(), level.Id); // Check name uniqueness string name_Temp = name; int count = 0; while (viewPlans.Find(x => x.Name == name_Temp) != null) { count++; name_Temp = string.Format("{0} {1}", name, count); } viewPlan_New.Name = name_Temp; } foreach (Parameter parameter in viewPlan.ParametersMap) { if (parameter.Id.IntegerValue == (int)BuiltInParameter.VIEW_NAME) { continue; } Definition definition = parameter?.Definition; if (definition == null) { continue; } Parameter parameter_New = viewPlan_New.get_Parameter(parameter.Definition); if (parameter_New == null) { continue; } CopyValue(parameter, parameter_New); } if (elements_ColorFillLegend != null && elements_ColorFillLegend.Count != 0) { ElementTransformUtils.CopyElements(viewPlan, elements_ColorFillLegend.ConvertAll(x => x.Id), viewPlan_New, null, new CopyPasteOptions()); } result.Add(viewPlan_New); IEnumerable <ElementId> elementIds_Dependent = viewPlan.GetDependentViewIds(); if (elementIds_Dependent != null && elementIds_Dependent.Count() != 0) { foreach (ElementId elementId_Dependent in elementIds_Dependent) { ViewPlan viewPlan_Dependent = document.GetElement(elementId_Dependent) as ViewPlan; if (viewPlan_Dependent == null) { continue; } viewPlans = new FilteredElementCollector(document).OfClass(typeof(ViewPlan)).Cast <ViewPlan>().ToList(); string name_Dependent = name; if (viewPlan_Dependent.Name.StartsWith(viewPlan.Name)) { name_Dependent = level.Name + viewPlan_Dependent.Name.Substring(viewPlan.Name.Length); } else { Element scopeBox = viewPlan_Dependent.ScopeBox(); if (scopeBox != null) { name_Dependent = level.Name + " - " + scopeBox.Name; } } ViewPlan viewPlan_Dependent_New = null; if (useExisting) { viewPlan_Dependent_New = viewPlans.Find(x => x.Name == name_Dependent); } if (viewPlan_Dependent_New == null) { ElementId elementId_Dependent_New = viewPlan_New.Duplicate(ViewDuplicateOption.AsDependent); if (elementId_Dependent_New == null || elementId_Dependent_New == ElementId.InvalidElementId) { continue; } viewPlan_Dependent_New = document.GetElement(elementId_Dependent_New) as ViewPlan; if (viewPlan_Dependent_New == null) { continue; } // Check name uniqueness string name_Dependent_Temp = name_Dependent; int count = 0; while (viewPlans.Find(x => x.Name == name_Dependent_Temp) != null) { count++; name_Dependent_Temp = string.Format("{0} {1}", name_Dependent, count); } viewPlan_Dependent_New.Name = name_Dependent_Temp; } foreach (Parameter parameter in viewPlan_Dependent.ParametersMap) { if (parameter.Id.IntegerValue == (int)BuiltInParameter.VIEW_NAME) { continue; } Definition definition = parameter?.Definition; if (definition == null) { continue; } Parameter parameter_New = viewPlan_Dependent_New.get_Parameter(parameter.Definition); if (parameter_New == null) { continue; } CopyValue(parameter, parameter_New); } result.Add(viewPlan_Dependent_New); } } } return(result); }
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { UIDocument uiDoc = commandData.Application.ActiveUIDocument; Document doc = commandData.Application.ActiveUIDocument.Document; ViewPlan activeview = uiDoc.ActiveView as ViewPlan; if (activeview == null) { TaskDialog.Show("wrong", "请在平面视图内运行."); return(Result.Failed); } Parameter para = activeview.LookupParameter("VIEW-Content"); if (para == null) { TaskDialog.Show("wrong", "未找到VIEW-Content参数"); return(Result.Failed); } //找到提资视图样板 ViewPlan template = null; FilteredElementCollector collector_template = new FilteredElementCollector(doc); collector_template.OfClass(typeof(ViewPlan)); IEnumerable <ViewPlan> templates = from ele in collector_template let view = ele as ViewPlan where view.Name.Equals("A-PLAN-MECH-INTF") where view.IsTemplate select view; if (templates != null && templates.Count() != 0) { template = templates.First(); } //找到所有的非建筑链接文件 FilteredElementCollector linkcollector = new FilteredElementCollector(doc); linkcollector.OfCategory(BuiltInCategory.OST_RvtLinks).WhereElementIsNotElementType(); IEnumerable <RevitLinkInstance> links = from ele in linkcollector let ins = ele as RevitLinkInstance let name = ins.Name where name[0] != 'A' select ins; //获取所有PLOT视图 List <ViewPlan> plots = GetPLOT(doc); if (plots.Count() == 0) { TaskDialog.Show("wrong", "未找到PLOT视图.确认视图名称内含PLOT,并且VIEW-Content参数设置为02.PLOT"); return(Result.Failed); } //让用户选择哪几个PLOT视图需要生成INTF视图. List <ViewPlan> plots_selected = new List <ViewPlan>(); MyWindow window = new MyWindow(); window.listbox.ItemsSource = plots; if (window.ShowDialog() == true) { var objs = window.listbox.SelectedItems; foreach (Object obj in objs) { plots_selected.Add(obj as ViewPlan); } } if (plots_selected.Count == 0) { return(Result.Failed); } //记录对应的intf视图. List <ViewPlan> intfs = new List <ViewPlan>(); //记录没有对应intf视图的plot视图. List <ViewPlan> plots_withoutintf = new List <ViewPlan>(); string name_intfs = " "; foreach (ViewPlan plot in plots_selected) { ViewPlan intf = FindINTFByPLOT(plot); if (intf == null) { plots_withoutintf.Add(plot); } else { intfs.Add(intf); name_intfs = name_intfs + "\n" + intf.Name; } } name_intfs += "\n以上INTF视图已生成,将删除现有视图并重新生成.其间可能出现Revit未响应的情况,请不要惊慌,去倒杯茶回来就好了."; if (intfs.Count != 0) { TaskDialog.Show("goa", name_intfs); } //预先设置视图样板,以保证后面copy 2D图元的时候减少错误的发生. if (template != null) { using (Transaction transaction_settemplate = new Transaction(doc)) { transaction_settemplate.Start("set template"); foreach (View intf in intfs) { intf.ViewTemplateId = template.Id; } transaction_settemplate.Commit(); } } //开启事务 将PLOT的2D图元更新到INTF using (Transaction transaction_delete2D = new Transaction(doc)) { transaction_delete2D.Start("delete 2d "); List <ElementId> deleteids = new List <ElementId>(); foreach (ViewPlan intf in intfs) { deleteids.AddRange(FindAll2DElmentIds(intf)); } doc.Delete(deleteids); transaction_delete2D.Commit(); } using (Transaction transaction_copy2D = new Transaction(doc)) { transaction_copy2D.Start("copy 2d "); string errinfo = "以下图元在无法复制:"; foreach (ViewPlan intf in intfs) { ViewPlan plot = FindPLOTByINTF(intf); if (plot == null) { continue; } List <ElementId> ids_2D = FindAll2DElmentIds(plot); Transform transform = ElementTransformUtils.GetTransformFromViewToView(plot, intf); //ElementTransformUtils.CopyElements(plot, ids_2D, intf, transform, new CopyPasteOptions()); foreach (ElementId id in ids_2D) { List <ElementId> ids = new List <ElementId>(); ids.Add(id); try { ElementTransformUtils.CopyElements(plot, ids, intf, transform, new CopyPasteOptions()); } catch { errinfo = errinfo + "\n ID:" + id.IntegerValue + "类别:" + doc.GetElement(id).Category.Name; continue; } } } TaskDialog.Show("goa", errinfo); transaction_copy2D.Commit(); } //开启事务 带细节复制plot视图 using (Transaction transaction_duplicate = new Transaction(doc)) { transaction_duplicate.Start("带细节复制plot视图"); foreach (ViewPlan plot in plots_withoutintf) { ElementId id_newintf = plot.Duplicate(ViewDuplicateOption.WithDetailing); ViewPlan newintf = doc.GetElement(id_newintf) as ViewPlan; //重命名视图名字 newintf.Name = plot.Name.Replace("PLOT", "INTF"); //共享参数VIEW-Content中 PLOT修改为INTF Parameter view_content = newintf.LookupParameter("VIEW-Content"); view_content.Set("03.INTF"); } transaction_duplicate.Commit(); } string wrong = "以下PLOT视图似乎存在问题,请手动进行提资操作."; using (Transaction transaction = new Transaction(doc)) { transaction.Start("deal"); foreach (ViewPlan plot in plots_selected) { ViewPlan intf = FindINTFByPLOT(plot); if (intf == null) { wrong = wrong + "\n" + plot.Name; continue; } HideHDDNTextNote(intf); HideHDDNAnnotation(intf); HideANNODimension(intf); //应用视图样板 if (template != null) { intf.ViewTemplateId = template.Id; } //关闭非建筑链接 List <ElementId> links_tohidden = new List <ElementId>(); foreach (RevitLinkInstance rli in links) { if (rli == null) { continue; } if (rli.CanBeHidden(intf)) { links_tohidden.Add(rli.Id); } } intf.HideElements(links_tohidden); } transaction.Commit(); } return(Result.Succeeded); }
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { _uiapp = commandData.Application; _uidoc = _uiapp.ActiveUIDocument; _doc = _uidoc.Document; try { // Get list of all structural levels IList <Level> strLevels = GetAllLevels(_doc, true, true); if (strLevels.Count == 0) { return(Result.Cancelled); } // Get list of all zone de définition IList <Element> zoneList = GetAllBuildingZones(_doc); if (zoneList.Count == 0) { return(Result.Cancelled); } int zoneCount = zoneList.Count; // Num of duplicata // Get the selected view ICollection <ElementId> refIds = _uidoc.Selection.GetElementIds(); if (refIds.Count == 0) { TaskDialog.Show("Revit", "Selecionner au moins d'une vue à dupliquer"); return(Result.Cancelled); } foreach (ElementId refId in refIds) { Element ele = _doc.GetElement(refId); if (ele.Category == null || !ele.Category.Name.Equals(Properties.Settings.Default.CATEGORY_NAME_VIEW)) { TaskDialog.Show("Revit", "Les élément selectionnés doivent être des VUES"); return(Result.Cancelled); } View view = ele as View; ViewFamilyType viewType = _doc.GetElement(view.GetTypeId()) as ViewFamilyType; if (!viewType.FamilyName.Equals(Properties.Settings.Default.FAMILY_TYPE_NAME_STR_PLAN)) { TaskDialog.Show("Revit", $"Les élément selectionnés doivent être des VUES EN PLAN"); return(Result.Cancelled); } } foreach (ElementId refId in refIds) { ViewPlan viewToDuplicate = _doc.GetElement(refId) as ViewPlan; Level level = viewToDuplicate.GenLevel; String levelName = level.Name; Transaction t = new Transaction(_doc, "Duplicate view"); t.Start(); for (int i = 0; i < zoneCount; i++) { ElementId viewId = viewToDuplicate.Duplicate(ViewDuplicateOption.AsDependent); ViewPlan duplicatedView = _doc.GetElement(viewId) as ViewPlan; duplicatedView.Name = $"{levelName} - {zoneList[i].Name}"; duplicatedView.get_Parameter(BuiltInParameter.VIEWER_VOLUME_OF_INTEREST_CROP).Set(zoneList[i].Id); // Associate level to the new viewplan AssociateLevelToNewViewPlan(level, duplicatedView); } t.Commit(); } return(Result.Succeeded); } catch (Autodesk.Revit.Exceptions.OperationCanceledException) { return(Result.Cancelled); } }