Revit API development (8): DirectShape create Revit geometry can be identified

Foreword

There are several ways Revit geometry may be introduced, but which is absolutely special DirectShape one. It generated with geometry, just like native Revit. Of course, in order to achieve the purpose of "native" in the program you will inevitably need to follow the rules you define it to be written.
Below, I'll explain a few examples.

Creating a cuboid with DirectShap

Creation process:

  • Create a square bottom
    • Four vertices, by four lines and then create vertices
  • Stretch out a cuboid
    • GeometryCreationUtilities.CreateExtrusionGeometry
      Note:
  1. Vertex order to pay attention, to create a curveloop
  2. DirectShape is a class, you can set different Category
  3. Transaction must use, any action to change the document require the use of Transaction

Also, if you want to create more complex graphics, and want to go to control every detail, that this case is definitely not enough. You need to use BRepBuilder.

public void CreateCubicDirectShape(Document doc)
{
    List<Curve> profile = new List<Curve>();

    double edgeLength = 2.0;    
    XYZ p1 = new XYZ(edgeLength, 0, 0);
    XYZ p2 = new XYZ(edgeLength, edgeLength, 0);
    XYZ p3 = new XYZ(0, edgeLength, 0);
    XYZ p4 = new XYZ(0, 0, 0);
	
    profile.Add(Line.CreateBound(p1, p2));
    profile.Add(Line.CreateBound(p2, p3));
    profile.Add(Line.CreateBound(p3, p4));
    profile.Add(Line.CreateBound(p4, p1));
		
    CurveLoop curveLoop = CurveLoop.Create(profile);
    SolidOptions options = new SolidOptions(ElementId.InvalidElementId, ElementId.InvalidElementId);
    Solid cubic = GeometryCreationUtilities.CreateExtrusionGeometry(new CurveLoop[] { curveLoop }, XYZ.BasisZ, 10);
    using (Transaction t = new Transaction(doc, "Create sphere direct shape"))
    {
        t.Start();
        // create direct shape and assign the sphere shape
        DirectShape ds = DirectShape.CreateElement(doc, new ElementId(BuiltInCategory.OST_GenericModel));
        ds.ApplicationId = "Application id";
        ds.ApplicationDataId = "Geometry object id";
        ds.SetShape(new GeometryObject[] { cubic });
        t.Commit();
    }
}

Renderings:
Here Insert Picture Description

Published 29 original articles · won praise 12 · views 9000

Guess you like

Origin blog.csdn.net/weixin_44153630/article/details/104032375