Generate Direct Shape from Selected Wall Geometry

Generate Direct Shape from Selected Wall Geometry

zrodgersTSSSU
Advocate Advocate
1,954 Views
4 Replies
Message 1 of 5

Generate Direct Shape from Selected Wall Geometry

zrodgersTSSSU
Advocate
Advocate

Hey Everyone, So dynamo allows you to get an elements geometry and then create a direct shape from the returned geometry. I am trying to do that same thing with API. I want to select a wall get its geometry and then create a direct shape that reflects all the openings in the wall.  I originally was going to copy and paste the wall in place then rotate it but revit doesnt alow you to rotate walls in the direction i want. 

 

zrodgersTSSSU_1-1622128417720.png

 

 

 

 

using (Transaction tx = new Transaction(doc))
                        {
                            tx.Start("Generate Tilt Wall");
                            foreach (ElementId elemId in selectedIds)
                            {
                                try
                                {
                                    Element elem = uidoc.Document.GetElement(elemId);
                                    if((BuiltInCategory)elem.Category.Id.IntegerValue == BuiltInCategory.OST_Walls)
                                    {
                                        Options opts = new Options();
                                        GeometryElement geoEle = elem.get_Geometry(opts);

                                        
                                        XYZ newloc = new XYZ(0, 0, 0);

                                        //gets wall curve for rotation
                                        Curve eleCurve = (elem.Location as LocationCurve).Curve;
                                        XYZ startPoint = eleCurve.GetEndPoint(0);
                                        XYZ endPoint = eleCurve.GetEndPoint(1);

                                        //need to set this equal to something so i can modify it
                                        //ICollection<ElementId> newElemColl = ElementTransformUtils.CopyElement(doc, elemId, newloc);
                                        //ElementId newEleId = newElemColl.First();

                                        XYZ pt1 = new XYZ(0, 0, 0);
                                        XYZ pt2 = new XYZ(0, 0, 10);
                                        Line axis = Line.CreateBound(pt1, pt2);

                                        //ElementTransformUtils.RotateElement(doc, newEleId, axis, rotAngle);

                                        DirectShape ds = DirectShape.CreateElement(doc, new ElementId(BuiltInCategory.OST_Walls));
                                        ds.ApplicationId = "Application id";
                                        ds.ApplicationDataId = "Geometry object id";
                                        ds.SetShape(new GeometryObject[] { geoEle });

                                       
                                        

                                    }

                                }
                                catch(Exception e) { throw e; }
                            }

                            


                            tx.Commit();
                        }

 

 

 

 

error:

zrodgersTSSSU_0-1622128329531.png

 

 

0 Likes
Accepted solutions (1)
1,955 Views
4 Replies
Replies (4)
Message 2 of 5

RPTHOMAS108
Mentor
Mentor

From DirectShapeType.SetShape  method noted in Revit API.chm:

 

pGeomArr
Type: System.Collections.Generic.IList<(Of <(<'GeometryObject>)>)>
Shape of this object expressed as a collection of GeometryObjects. These will be copied. Shape and Category should be consistent: geometry supplied as shape should be valid for the Category the type object is associated with. The supported types of GeometryObjects are: Solid, Mesh, GeometryInstance, Point, Curve and PolyLine.

 

i.e. GeometryElement (a collection of primitives) is not a supported GeometryObject.

 

You have to take the items out of it that are supported above and put them in your input list of GeometryObject.

0 Likes
Message 3 of 5

zrodgersTSSSU
Advocate
Advocate

@RPTHOMAS108 thank you for the quick response. I got the solids and created the direct shape this way. 

zrodgersTSSSU_0-1622134560911.png

Element elem = uidoc.Document.GetElement(elemId);
                                    if((BuiltInCategory)elem.Category.Id.IntegerValue == BuiltInCategory.OST_Walls)
                                    {
                                        
                                        Options opts = new Options();
                                        opts.ComputeReferences = true;
                                        opts.IncludeNonVisibleObjects = true;
                                        
                                        GeometryElement geoEle = elem.get_Geometry(opts);

                                        foreach (GeometryObject geomObj in geoEle)
                                        {
                                            Solid geomSolid = geomObj as Solid;
                                            if (null != geomSolid)
                                            {

                                                DirectShape ds = DirectShape.CreateElement(doc, new ElementId(BuiltInCategory.OST_Walls));
                                                ds.ApplicationId = "Application id";
                                                ds.ApplicationDataId = "Geometry object id";
                                                ds.SetShape(new GeometryObject[] {geomSolid});
 
                                            }
                                        }

however... when i run it it gives me 4 extra solids.... seem to be faces. any idea how to only get the solid in the back?

zrodgersTSSSU_1-1622134687676.png

 

0 Likes
Message 4 of 5

RPTHOMAS108
Mentor
Mentor
Accepted solution

You are including non-visible objects:

 

opts.IncludeNonVisibleObjects = true

 

I would set that to false, have a look in RevitLookup to try an understand their meaning. Could be related to layers or references within the wall (not necessarily useful).

0 Likes
Message 5 of 5

zrodgersTSSSU
Advocate
Advocate

Thank you that worked!!! 

0 Likes