Skip to content

Instantly share code, notes, and snippets.

@pzurek
Created September 4, 2012 22:13
Show Gist options
  • Save pzurek/3627249 to your computer and use it in GitHub Desktop.
Save pzurek/3627249 to your computer and use it in GitHub Desktop.
try {
if (null == commandData) {
throw new ArgumentNullException("commandData");
}
UIApplication app = commandData.Application;
UIDocument uiDoc = app.ActiveUIDocument;
Document doc = uiDoc.Document;
using (Transaction transaction = new Transaction(doc)) {
transaction.Start("Building panels");
ISelectionFilter wallFilter = new WallSelectionFilter();
Reference wallReference = uiDoc.Selection.PickObject(ObjectType.Element,
wallFilter,
"Select a wall to split into panels");
if (wallReference != null && wallReference.ElementId != ElementId.InvalidElementId) {
Wall wall = doc.GetElement(wallReference.ElementId) as Wall;
if (wall == null) {
return Result.Failed;
}
IList<ElementId> wallList = new List<ElementId>();
wallList.Add(wallReference.ElementId);
LocationCurve lc = wall.Location as LocationCurve;
Line line = lc.Curve as Line;
if (null == line) {
message = "Unable to retrieve wall location line.";
return Result.Failed;
}
XYZ origin = new XYZ(line.Origin.X, line.Origin.Y, line.Origin.Z);
int divisions = 5;
IList<XYZ> divisionPoints = new List<XYZ>();
IList<XYZ> intersectionLineOriginPoints = new List<XYZ>();
IList<XYZ> intersectionLineEndPoints = new List<XYZ>();
for (var i = 1; i < divisions; i++) {
XYZ newPoint = new XYZ();
newPoint = origin.Add(line.Direction.Multiply(line.Length * i / divisions));
divisionPoints.Add(newPoint);
}
// Creating a vector colinear with the wall with length equal 2* the wall witdh
XYZ wallWidthVector = line.Direction.Multiply(2 * wall.Width);
IList<ElementId> intersectionElementIds = new List<ElementId>();
IList<Curve> curveArray = new List<Curve>();
for (var i = 0; i < divisions - 1; i++) {
// Creating a line for each division point
// Startpoint at (division point - wall width vector)
// Endpoint at (division point + wall width vector)
Line intersectionLine = Line.CreateBound(divisionPoints[i].Add(wallWidthVector.Negate()),
divisionPoints[i].Add(wallWidthVector));
// Creating a 90deg rotation transform
Transform rotate90Deg = Transform.CreateRotationAtPoint(XYZ.BasisZ,
Units.ToRadians(90),
divisionPoints[i]);
// Based on intersectionLines creating a 90deg rotated curve
// This curve, perpendicular to the wall will be used to define 2 things:
// the curve dividing the object
Curve intersectionCurve = intersectionLine.CreateTransformed(rotate90Deg);
// the reference plane dividing the object
ReferencePlane intersectionPlane = doc.Create.NewReferencePlane2(intersectionCurve.GetEndPoint(0),
intersectionCurve.GetEndPoint(1),
line.Direction,
doc.ActiveView);
curveArray.Add(intersectionCurve);
intersectionElementIds.Add(intersectionPlane.Id);
}
SketchPlane divisionSketchPlane = SketchPlane.Create(doc, new Plane(new XYZ(0, 0, 1), line.Origin));
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)) {
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment