Skip to content

Instantly share code, notes, and snippets.

@pzurek
Created September 4, 2012 20:55
Show Gist options
  • Save pzurek/3626346 to your computer and use it in GitHub Desktop.
Save pzurek/3626346 to your computer and use it in GitHub Desktop.
try
{
if( null == commandData )
{
throw new ArgumentNullException(
"commandData" );
}
UIApplication uiapp = commandData.Application;
Application app = uiapp.Application;
UIDocument uidoc = uiapp.ActiveUIDocument;
Document doc = uidoc.Document;
using( Transaction tx = new Transaction( doc ) )
{
tx.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;
}
int divisions = 5;
IList<XYZ> divisionPoints
= new List<XYZ>();
IList<XYZ> intersectionLineOriginPoints
= new List<XYZ>();
IList<XYZ> intersectionLineEndPoints
= new List<XYZ>();
XYZ origin = line.Origin;
XYZ v = line.Direction.Multiply(
line.Length / divisions );
for( int i = 1; i < divisions; ++i )
{
divisionPoints.Add( origin + i * v );
}
// Double the width of the wall
XYZ wallWidthVector = line.Direction
.Multiply( 2 * wall.Width );
IList<ElementId> intersectionElementIds
= new List<ElementId>();
IList<Curve> curveArray = new List<Curve>();
for( int i = 0; i < divisions - 1; ++i )
{
Line intersectionLine
= app.Create.NewLineBound(
divisionPoints[i].Add(
wallWidthVector.Negate() ),
divisionPoints[i].Add(
wallWidthVector ) );
Transform rotate90Deg
= Transform.get_Rotation(
divisionPoints[i], XYZ.BasisZ,
DegreesToRadians( 90 ) );
Curve intersectionCurve
= intersectionLine.get_Transformed(
rotate90Deg );
curveArray.Add( intersectionCurve );
XYZ startPoint = intersectionCurve
.get_EndPoint( 0 );
XYZ endPoint = intersectionCurve
.get_EndPoint( 1 );
ReferencePlane intersectionPlane
= doc.Create.NewReferencePlane2(
startPoint, endPoint,
line.Direction, doc.ActiveView );
intersectionElementIds.Add(
intersectionPlane.Id );
}
SketchPlane divisionSketchPlane
= doc.Create.NewSketchPlane(
app.Create.NewPlane(
XYZ.BasisZ, 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 );
}
}
}
tx.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