public static List <global::Revit.Elements.Element> CreateParts(global::Revit.Elements.Element element) { Autodesk.Revit.DB.Document doc = DocumentManager.Instance.CurrentDBDocument; //create a list to hold the element ids and add them to it ICollection <Autodesk.Revit.DB.ElementId> elementIds = new List <ElementId>(); elementIds.Add(element.InternalElement.Id); //start a transaction and create the parts TransactionManager.Instance.EnsureInTransaction(doc); PartUtils.CreateParts(doc, elementIds); TransactionManager.Instance.TransactionTaskDone(); doc.ActiveView.PartsVisibility = PartsVisibility.ShowPartsOnly; //regeneration allows the parts to be selectable doc.Regenerate(); //collect the parts that have been created ICollection <ElementId> partIds = PartUtils.GetAssociatedParts(doc, element.InternalElement.Id, false, false); List <global::Revit.Elements.Element> partList = new List <global::Revit.Elements.Element>(); //collect the newly created parts foreach (Autodesk.Revit.DB.ElementId id in partIds) { partList.Add(doc.GetElement(id).ToDSType(true)); } return(partList); }
public bool Create() { IList <ElementId> idList = new List <ElementId>(); ICollection <ElementId> elementIdsToDivide = new List <ElementId>(); idList.Add(Floor.Id); if (PartUtils.AreElementsValidForCreateParts(PublicVariables.Doc, idList)) { using (Transaction tr = new Transaction(PublicVariables.Doc, "CreatePart")) { tr.Start(); try { PartUtils.CreateParts(PublicVariables.Doc, idList); tr.Commit(); } catch { tr.RollBack(); } elementIdsToDivide = PartUtils.GetAssociatedParts(PublicVariables.Doc , Floor.Id, true, true); } } ICollection <ElementId> intersectingReferenceIds = new List <ElementId>(); var curveList = (from Curve elem in BoundingSquare select elem).ToList(); using (Transaction tr = new Transaction(PublicVariables.Doc, "DivideParts")) { tr.Start(); try { SketchPlane sp = SketchPlane.Create(PublicVariables.Doc, Plane.CreateByNormalAndOrigin(new XYZ(0, 0, 1), BoundingSquare[0].GetEndPoint(0))); PartMaker maker = PartUtils.DivideParts(PublicVariables.Doc, elementIdsToDivide, intersectingReferenceIds, curveList, sp.Id); Parameter partVisInView = PublicVariables.Doc.ActiveView.get_Parameter(BuiltInParameter.VIEW_PARTS_VISIBILITY); partVisInView.Set(0); tr.Commit(); } catch { tr.RollBack(); } } return(true); }
public Result Execute( ExternalCommandData cmdData, ref string msg, ElementSet elems) { Result result = Result.Failed; UIApplication uiApp = cmdData.Application; UIDocument uiDoc = uiApp.ActiveUIDocument; Document doc = uiDoc.Document; Transaction transaction = new Transaction(doc); try { string strMsg = "Select walls"; ISelectionFilter filter = new WallSelectionFilter(); IList <Reference> walls = uiDoc.Selection.PickObjects( ObjectType.Element, filter, strMsg); if (walls.Count == 0) { return(result); } List <ElementId> ids = new List <ElementId>(); foreach (Reference reference in walls) { ids.Add(reference.ElementId); } if (!PartUtils.AreElementsValidForCreateParts( doc, ids)) { return(result); } transaction.Start("parts"); // Split walls into parts PartUtils.CreateParts(doc, ids); // Regenerate document to get the part geometry doc.Regenerate(); // Retrieve points from bottom faces of parts List <List <XYZ> > bottomFacesPts = new List <List <XYZ> >(); foreach (ElementId id in ids) { if (!PartUtils.HasAssociatedParts(doc, id)) { continue; } ICollection <ElementId> partIds = PartUtils.GetAssociatedParts( doc, id, true, true); foreach (ElementId partId in partIds) { Element part = doc.GetElement(partId); bottomFacesPts.Add( GetBottomFacePoints(part)); } } // Do not affect the original state of walls transaction.RollBack(); // Draw lines to show the bottom faces of parts transaction.Start(); ModelLineCreator model = new ModelLineCreator(doc); foreach (List <XYZ> bottomFacePts in bottomFacesPts) { for (int i = 1; i < bottomFacePts.Count; ++i) { model.CreateLine(bottomFacePts[i - 1], bottomFacePts[i], true); } if (bottomFacePts.Count > 3) { model.CreateLine(bottomFacePts[0], bottomFacePts[bottomFacePts.Count - 1], true); } } transaction.Commit(); result = Result.Succeeded; } catch (System.Exception e) { msg = e.Message; result = Result.Failed; } return(result); }
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { string input = Interaction.InputBox("Enter the panel width in millimeters", "CLT Creator", "250", -1, -1); double inputnum = Convert.ToDouble(input); /* * var hi = new TaskDialog("Hi") * { * MainContent = inputnum, * MainIcon = TaskDialogIcon.TaskDialogIconInformation, * CommonButtons = TaskDialogCommonButtons.Ok * }; * hi.Show(); */ try { if (null == commandData) { throw new ArgumentNullException("commandData"); } UIApplication uiapp = commandData.Application; Application app = uiapp.Application; UIDocument uidoc = uiapp.ActiveUIDocument; Document doc = uidoc.Document; Reference r = null; try { r = uidoc.Selection.PickObject( ObjectType.Element, new WallSelectionFilter(), "Select a wall to split into panels"); } catch (Autodesk.Revit.Exceptions .OperationCanceledException) { return(Result.Cancelled); } Wall wall = (r == null || r.ElementId == ElementId.InvalidElementId) ? null : doc.GetElement(r.ElementId) as Wall; if (wall == null) { message = "Unable to retrieve wall."; return(Result.Failed); } LocationCurve location = wall.Location as LocationCurve; if (null == location) { message = "Unable to retrieve wall location curve."; return(Result.Failed); } Line line = location.Curve as Line; if (null == location) { message = "Unable to retrieve wall location line."; return(Result.Failed); } using (Transaction transaction = new Transaction(doc)) { transaction.Start("Building panels"); IList <ElementId> wallList = new List <ElementId>(1); wallList.Add(r.ElementId); if (PartUtils.AreElementsValidForCreateParts( doc, wallList)) { PartUtils.CreateParts(doc, wallList); doc.Regenerate(); ICollection <ElementId> parts = PartUtils.GetAssociatedParts( doc, wall.Id, false, false); if (PartUtils.ArePartsValidForDivide( doc, parts)) { double divisions = line.Length / (inputnum / 25.4 / 12); XYZ origin = line.Origin; XYZ delta = line.Direction.Multiply( inputnum / 25.4 / -12); Transform shiftDelta = Transform.CreateTranslation(delta); // Construct a 90 degree rotation in the // XY plane around the line start point Transform rotation = Transform.CreateRotationAtPoint( XYZ.BasisZ, 0.5 * Math.PI, origin); // A vector perpendicular to the wall with // length equal to twice the wall witdh XYZ wallWidthVector = rotation.OfVector( line.Direction.Multiply(2 * wall.Width)); Curve intersectionLine = Line.CreateBound( // Line.CreateBound location.Curve.GetEndPoint(1) + wallWidthVector, location.Curve.GetEndPoint(1) - wallWidthVector); IList <Curve> curveArray = new List <Curve>(); //Jeremy's Modelline Code //XYZ v = 2*wallWidthVector; //double dxy = Math.Abs(v.X) + Math.Abs(v.Y); //XYZ w = (dxy > 1.0e-9)? XYZ.BasisZ: XYZ.BasisY; //XYZ norm = v.CrossProduct(w).Normalize(); //Plane plane = Plane.CreateByNormalAndOrigin(norm, origin + wallWidthVector); //SketchPlane sketchPlane = SketchPlane.Create(doc, plane); //Jeremy's code end for (int i = 1; i < divisions; ++i) { intersectionLine = intersectionLine.CreateTransformed(shiftDelta); //ModelCurve curve = doc.IsFamilyDocument ? doc.FamilyCreate.NewModelCurve(intersectionLine, sketchPlane) : doc.Create.NewModelCurve(intersectionLine, sketchPlane); curveArray.Add(intersectionLine); } SketchPlane divisionSketchPlane = SketchPlane.Create(doc, Plane.CreateByNormalAndOrigin(XYZ.BasisZ, origin)); // An empty list of intersecting ElementIds IList <ElementId> intersectionElementIds = new List <ElementId>(); PartUtils.DivideParts(doc, parts, intersectionElementIds, curveArray, divisionSketchPlane.Id); } doc.ActiveView.PartsVisibility = PartsVisibility.ShowPartsOnly; } transaction.Commit(); } return(Result.Succeeded); } catch (Exception e) { message = e.Message; return(Result.Failed); } }
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; View view = doc.ActiveView; //TaskDialog.Show("Revit", "Hello World"); // Create filters and collect walls/ slabs/ columns id in corresponding collector // walls IList <ElementId> walls_id = new List <ElementId>(); FilteredElementCollector wall_collector = new FilteredElementCollector(doc).OfClass(typeof(Wall)); foreach (Element w in wall_collector) { if (w is Wall) { walls_id.Add(w.Id); } } // columns IList <ElementId> columns_id = new List <ElementId>(); FilteredElementCollector column_collector = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_StructuralColumns); foreach (Element c in column_collector) { if (c is FamilyInstance) { columns_id.Add(c.Id); } } // slabs IList <ElementId> slabs_id = new List <ElementId>(); FilteredElementCollector collector = new FilteredElementCollector(doc).OfClass(typeof(Floor)); foreach (Element e in collector) { if (e is Floor) { slabs_id.Add(e.Id); } } //call create parts on walls/ slabs/ columns collectors using (Transaction t = new Transaction(doc, "Create Part")) { t.Start(); // Create parts from the selected element // There is "CreateParts" but no "CreatePart", so needed to use a list containing the one element PartUtils.CreateParts(doc, walls_id); PartUtils.CreateParts(doc, columns_id); PartUtils.CreateParts(doc, slabs_id); t.Commit(); } // start divide parts for walls, columns, slabs foreach (ElementId w_id in walls_id) { ICollection <ElementId> partsList = PartUtils.GetAssociatedParts(doc, w_id, true, true); // Get all levels ICollection <ElementId> levels = new FilteredElementCollector(doc).OfClass(typeof(Level)).OfCategory(BuiltInCategory.OST_Levels).ToElementIds(); // Create a list of curves which needs to be used in DivideParts but for this example // the divide is being done by levels so the curve list will be empty IList <Curve> curve_list = new List <Curve>(); // Get the host object corresponding to the selected element // HostObject is the parent class for walls, roof, floors, etc. HostObject hostObj = doc.GetElement(w_id) as HostObject; // Get the reference of one of the major faces of the selected element // Will be used to create a sketch plane Reference r = HostObjectUtils.GetSideFaces(hostObj, ShellLayerType.Exterior).First(); using (Transaction t = new Transaction(doc, "Divide Part at Levels")) { t.Start(); //Plane ref_plane = Plane.CreateByNormalAndOrigin(faceNormal, XYZ.Zero); SketchPlane wall_sketchPlane = SketchPlane.Create(doc, r); //SketchPlane sketchPlane = doc.Create.NewSketchPlane(r); // Divide the parts PartUtils.DivideParts(doc, partsList, levels, curve_list, wall_sketchPlane.Id); t.Commit(); } } // since walls and columns are all divided by all levels, so just use the sketch-plane of the last wall element ElementId borrow_from_wall = walls_id[0]; foreach (ElementId c_id in columns_id) { ICollection <ElementId> partsList = PartUtils.GetAssociatedParts(doc, c_id, true, true); ICollection <ElementId> levels = new FilteredElementCollector(doc).OfClass(typeof(Level)).OfCategory(BuiltInCategory.OST_Levels).ToElementIds(); IList <Curve> curve_list = new List <Curve>(); HostObject hostObj = doc.GetElement(borrow_from_wall) as HostObject; Reference r = HostObjectUtils.GetSideFaces(hostObj, ShellLayerType.Exterior).First(); using (Transaction t = new Transaction(doc, "Divide Part at Levels")) { t.Start(); SketchPlane column_sketchPlane = SketchPlane.Create(doc, r); PartUtils.DivideParts(doc, partsList, levels, curve_list, column_sketchPlane.Id); t.Commit(); } } foreach (ElementId s_id in slabs_id) { //Selection sel = uidoc.Selection; //ISelectionFilter f = new JtElementsOfClassSelectionFilter<Grid>(); //Reference elemRef = sel.PickObject(ObjectType.Element, f, "Pick a grid"); //Grid grid = doc.GetElement(elemRef) as Grid; //ICollection<ElementId> grid_list = new List<ElementId>(); //grid_list.Add(grid.Id); //IList<Curve> gridCurves = grid.GetCurvesInView(DatumExtentType.Model, view); ICollection <ElementId> partsList = PartUtils.GetAssociatedParts(doc, s_id, true, true); // Get all levels ICollection <ElementId> grids = new FilteredElementCollector(doc).OfClass(typeof(Grid)).OfCategory(BuiltInCategory.OST_Grids).ToElementIds(); // Create a list of curves which needs to be used in DivideParts but for this example // the divide is being done by levels so the curve list will be empty IList <Curve> curve_list = new List <Curve>(); HostObject hostObj = doc.GetElement(s_id) as HostObject; Reference r = HostObjectUtils.GetTopFaces(hostObj).First(); using (Transaction t = new Transaction(doc, "Divide Part at Grids")) { t.Start(); //Transaction sketchPlaneTransaction = new Transaction(doc, "Create Sketch Plane"); SketchPlane grid_sketchPlane = SketchPlane.Create(doc, r); //SketchPlane grid_sketchPlane = null; //sketchPlaneTransaction.Commit(); PartUtils.DivideParts(doc, partsList, grids, curve_list, grid_sketchPlane.Id); t.Commit(); } } // Set the view's "Parts Visibility" parameter so that parts are shown Parameter p = doc.ActiveView.get_Parameter(BuiltInParameter.VIEW_PARTS_VISIBILITY); using (Transaction t = new Transaction(doc, "Set View Parameter")) { t.Start(); p.Set(0); // 0 = Show Parts, 1 = Show Original, 2 = Show Both t.Commit(); } //ICollection<ElementId> elementIdsToDivide = new List<ElementId>(); //if (PartUtils.AreElementsValidForCreateParts(doc, slabs_id)) //{ // // AreElementsValidForCreateParts returned true, so the selected element is not a part but it is an element that can be used to create a part. // Transaction createPartTransaction = new Transaction(doc, "Create Part"); // createPartTransaction.Start(); // PartUtils.CreateParts(doc, slabs_id); // create the parts // createPartTransaction.Commit(); // foreach (ElementId e_id in slabs_id) // { // elementIdsToDivide = PartUtils.GetAssociatedParts(doc, e_id, true, true); // }// get the id of the newly created part //} ////else if (pickedElement is Part) ////{ //// // The selected element is a part, so that part will be divided. //// elementIdsToDivide.Add(pickedElement.Id); ////} //// Create geometry that will be used to divide the part. For this example, a new part will be divided from the main part that is one quarter of the face. More complex intelligence could be coded to divide the part based on construction logistics or the properties of the materials being used to create the part. //XYZ pointRight = null; //XYZ pointTop = null; //XYZ pointCorner = null; //XYZ pointCenter = null; //SketchPlane sketchPlane = null; //Plane plane = null; //Options opt = new Options(); //opt.ComputeReferences = true; //foreach (Element e in slabs) //{ // GeometryElement geomElem = e.get_Geometry(opt); // foreach (GeometryObject geomObject in geomElem) // { // if (geomObject is Solid) // get the solid geometry of the selected element // { // Solid solid = geomObject as Solid; // FaceArray faceArray = solid.Faces; // foreach (Face face in faceArray) // { // // find the center of the face // BoundingBoxUV bbox = face.GetBoundingBox(); // UV center = new UV((bbox.Max.U - bbox.Min.U) / 2 + bbox.Min.U, (bbox.Max.V - bbox.Min.V) / 2 + bbox.Min.V); // XYZ faceNormal = face.ComputeNormal(center); // if (faceNormal.IsAlmostEqualTo(XYZ.BasisZ)) // this example is designed to work with a floor or other element with a large face whose normal is in the Z direction // { // Transaction sketchPlaneTransaction = new Transaction(doc, "Create Sketch Plane"); // sketchPlaneTransaction.Start(); // plane = Plane.CreateByNormalAndOrigin(faceNormal, XYZ.Zero); // sketchPlane = SketchPlane.Create(doc, plane); // //sketchPlane = doc.SketchPlane.Create(face as PlanarFace); // sketchPlaneTransaction.Commit(); // pointCenter = face.Evaluate(center); // UV top = new UV((bbox.Max.U - bbox.Min.U) / 2 + bbox.Min.U, bbox.Max.V); // pointTop = face.Evaluate(top); // UV right = new UV(bbox.Max.U, (bbox.Max.V - bbox.Min.V) / 2 + bbox.Min.V); // pointRight = face.Evaluate(right); // UV corner = new UV(bbox.Max.U, bbox.Max.V); // pointCorner = face.Evaluate(corner); // break; // } // } // } // } //} ////Selection sel = uidoc.Selection; ////Reference elemRef = sel.PickObject( ////ObjectType.Element, f, "Pick a grid"); ////Grid grid = doc.GetElement(elemRef) as Grid; //// Create the curves that will be used for the part division. //IList<Curve> curveList = new List<Curve>(); ////Curve curve1 = app.Create.NewLine(pointCenter, pointRight, true); //Curve curve1 = Line.CreateBound(pointCenter, pointRight); //curveList.Add(curve1); ////Curve curve2 = app.Create.NewLine(pointRight, pointCorner, true); //Curve curve2 = Line.CreateBound(pointRight, pointCorner); //curveList.Add(curve2); ////Curve curve3 = app.Create.NewLine(pointCorner, pointTop, true); //Curve curve3 = Line.CreateBound(pointCorner, pointTop); //curveList.Add(curve3); ////Curve curve4 = app.Create.NewLine(pointTop, pointCenter, true); //Curve curve4 = Line.CreateBound(pointTop, pointCenter); //curveList.Add(curve4); //// intersectingReferenceIds will be empty for this example. //ICollection<ElementId> intersectingReferenceIds = new List<ElementId>(); //// Divide the part //Transaction dividePartTransaction = new Transaction(doc, "Divide Part"); //dividePartTransaction.Start(); //PartMaker maker = PartUtils.DivideParts(doc, elementIdsToDivide, intersectingReferenceIds, curveList, sketchPlane.Id); //dividePartTransaction.Commit(); ////ICollection<ElementId> divElems = maker.GetSourceElementIds(); // Get the ids of the divided elements //// Set the view's "Parts Visibility" parameter so that parts are shown //Parameter partVisInView = doc.ActiveView.get_Parameter(BuiltInParameter.VIEW_PARTS_VISIBILITY); //Transaction setPartVizTransaction = new Transaction(doc, "Set View Parameter"); //setPartVizTransaction.Start(); //partVisInView.Set(0); // 0 = Show Parts, 1 = Show Original, 2 = Show Both //setPartVizTransaction.Commit(); ////// Access current selection return(Result.Succeeded); }
/// <summary> /// Divide part list for element. /// </summary> /// <param name="elm"></param> /// <param name="origin"></param> /// <param name="baseX"></param> /// <param name="step"></param> /// <param name="initAngle"></param> /// <param name="radius"></param> public static void DividePartList(this Element elm, XYZ origin, XYZ baseX, double[] step, TextureAngle initAngle, double radius = 5000) { if (elm is null) { throw new ArgumentNullException(nameof(elm)); } if (origin is null) { throw new ArgumentNullException(nameof(origin)); } if (baseX is null) { throw new ArgumentNullException(nameof(baseX)); } if (step[0] < 0) { throw new ArgumentException(nameof(step)); } if (step[1] < 0) { throw new ArgumentException(nameof(step)); } if (radius < 0) { throw new ArgumentException(nameof(radius)); } if (initAngle == Rotation90 || initAngle == Rotation270) { step = new[] { step[1], step[0] } } ; var plane = Plane.CreateByNormalAndOrigin(XYZ.BasisZ, origin); var baseY = plane.Normal.CrossProduct(baseX); var lines = new List <Curve>(); var xAxis = Line.CreateBound(origin - radius * baseX, origin + radius * baseX); var yAxis = Line.CreateBound(origin - radius * baseY, origin + radius * baseY); lines.Add(xAxis); lines.Add(yAxis); var yp0 = yAxis.GetEndPoint(0); var yp1 = yAxis.GetEndPoint(1); var xNum = Convert.ToInt32(Math.Ceiling(radius / step[0])); var yNum = Convert.ToInt32(Math.Ceiling(radius / step[1])); // Draws lines on x direction for (var i = 0; i < xNum; i++) { var offset = (i + 1) * step[0] * baseX; // On Right. lines.Add(Line.CreateBound(yp0 + offset, yp1 + offset)); // On Left. lines.Add(Line.CreateBound(yp0 - offset, yp1 - offset)); } var xp0 = xAxis.GetEndPoint(0); var xp1 = xAxis.GetEndPoint(1); // Draws lines on y direction for (var i = 0; i < yNum; i++) { var offset = (i + 1) * step[1] * baseY; // Above. lines.Add(Line.CreateBound(xp0 + offset, xp1 + offset)); // Below. lines.Add(Line.CreateBound(xp0 - offset, xp1 - offset)); } var doc = elm.Document; var sketchPlane = SketchPlane.Create(doc, plane); var ids = PartUtils.GetAssociatedParts(doc, elm.Id, true, false).ToList(); if (ids.Count > 0) { var partMaker = PartUtils.GetAssociatedPartMaker(doc, ids[0]); doc.Delete(partMaker.Id); } ids = PartUtils.GetAssociatedParts(doc, elm.Id, true, true).ToList(); if (ids.Count <= 0) { PartUtils.CreateParts(doc, new List <ElementId> { elm.Id }); doc.Regenerate(); ids = PartUtils.GetAssociatedParts(doc, elm.Id, true, true).ToList(); } if (ids.Count == 1) { PartUtils.DivideParts(doc, ids, new List <ElementId>(), lines, sketchPlane.Id); } }
/// <summary> /// Divides part list for wall. /// </summary> /// <param name="wall"></param> /// <param name="step"></param> /// <param name="materialNames"></param> /// <param name="lineHeight"></param> /// <param name="isHorizontal"></param> /// <param name="eps"></param> public static void DividePartList(this Wall wall, double[] step, string[] materialNames, double lineHeight, bool isHorizontal = true, double eps = 1e-3) { if (wall is null) { throw new ArgumentNullException(nameof(wall)); } if (step is null) { throw new ArgumentNullException(nameof(step)); } if (materialNames is null) { throw new ArgumentNullException(nameof(materialNames)); } var line = (wall.Location as LocationCurve)?.Curve as Line; var lines = new List <Curve>(); if (isHorizontal) { var p0 = line.GetEndPoint(0); var p1 = line.GetEndPoint(1); lines.Add(Line.CreateBound(p0, p1)); for (var i = 0; i < step.Length; i++) { var sum = step.Take(i + 1).Sum(); var tmpP0 = new XYZ(p0.X, p0.Y, p0.Z + sum); var tmpP1 = new XYZ(p1.X, p1.Y, p1.Z + sum); var tmpLine = Line.CreateBound(tmpP0, tmpP1); lines.Add(tmpLine); } } else { var p0 = line.GetEndPoint(0); var p1 = new XYZ(p0.X, p0.Y, lineHeight); lines.Add(Line.CreateBound(p0, p1)); for (var i = 0; i < step.Length; i++) { var sum = step.Take(i + 1).Sum(); var tmpP0 = p0 + sum * line.Direction; var tmpP1 = p1 + sum * line.Direction; var tmpLine = Line.CreateBound(tmpP0, tmpP1); lines.Add(tmpLine); } } var opt = new Options { ComputeReferences = true, DetailLevel = ViewDetailLevel.Coarse }; var ge = wall.get_Geometry(opt); var solid = ge.FirstOrDefault(f => f is Solid) as Solid; if (solid != null) { var faces = solid.Faces.Cast <Face>().Where(w => w is PlanarFace).Cast <PlanarFace>().ToList(); var face = faces.FirstOrDefault(f => f.FaceNormal.AngleTo(wall.Orientation) < eps); var doc = wall.Document; var plane = SketchPlane.Create(doc, face.Reference); var ids = PartUtils.GetAssociatedParts(doc, wall.Id, true, false).ToList(); if (ids.Count > 0) { var partMaker = PartUtils.GetAssociatedPartMaker(doc, ids[0]); doc.Delete(partMaker.Id); } ids = PartUtils.GetAssociatedParts(doc, wall.Id, true, true).ToList(); if (ids.Count <= 0) { PartUtils.CreateParts(doc, new List <ElementId> { wall.Id }); doc.Regenerate(); ids = PartUtils.GetAssociatedParts(doc, wall.Id, true, true).ToList(); } if (ids.Count == 1) { PartUtils.DivideParts(doc, ids, new List <ElementId>(), lines, plane.Id); } var materials = doc.GetInstanceList <Material>(); for (var i = 0; i < ids.Count; i++) { var part = doc.GetElement(ids[i]) as Part; if (part != null) { var parm = part.get_Parameter(BuiltInParameter.DPART_MATERIAL_BY_ORIGINAL); parm.Set(0); } var materialParm = part.get_Parameter(BuiltInParameter.DPART_MATERIAL_ID_PARAM); var materialId = materials.FirstOrDefault(f => f.Name == materialNames[i])?.Id; materialParm.Set(materialId); } } }