Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

using PlanarFace.GetEdgesAsCurveLoops and FilledRegion.Create together

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
Anonymous
891 Views, 3 Replies

using PlanarFace.GetEdgesAsCurveLoops and FilledRegion.Create together

Hi everyone, I'm trying to create a plugin that will allow me to create filled regions just by selecting a face. I use Selection.PickObject(ObjectType.Face), convert that reference to a PlanarFace type, and then use PlanarFace.GetEdgesAsCurveLoops to get the collection of CurveLoops and pass that to FilledRegion.Create to create my filled region.

 

However it's resulting in unexpected behavior. If I click on the face of a roof surface, it creates the FilledRegion exactly over the Face like I want it to. But when I click the Face of a Family Instance, it still creates the FilledRegion- but instead of creating it over the Face of the Family Instance, it creates the instance at the Project Origin of the Document.

 

I attached the code below and am also including some screenshots showing the problem in action. Does anyone know how to get around this? Thanks!

 

Screenshots here. 

 

    public class Command : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIApplication uiApp = commandData.Application;
            UIDocument uiDoc = uiApp.ActiveUIDocument;
            Document currentDoc = uiDoc.Document;
            Autodesk.Revit.DB.View currentView = uiDoc.ActiveView;
            Selection sel = uiApp.ActiveUIDocument.Selection;
            Transaction transaction = new Transaction(currentDoc);

            Element stringFillType = new FilteredElementCollector(currentDoc)
                .OfClass(typeof(FilledRegionType))
                .FirstOrDefault(x => x.Name == "String 1");
            
            transaction.Start("String fill");

            Reference moduleFaceReference = sel.PickObject(ObjectType.Face);
            GeometryObject geoObj = currentDoc.GetElement(moduleFaceReference).GetGeometryObjectFromReference(moduleFaceReference);
            PlanarFace moduleFace = geoObj as PlanarFace;
            IList<CurveLoop> faceEdges = moduleFace.GetEdgesAsCurveLoops();
            FilledRegion fillRegion = FilledRegion.Create(currentDoc, stringFillType.Id, currentView.Id, faceEdges);
            
            transaction.Commit();

            Result result = Result.Succeeded;
            return result;

            // throw new NotImplementedException();
        }
    }

 

3 REPLIES 3
Message 2 of 4
jeremytammik
in reply to: Anonymous

The family instance reuses the geometry defined for the family symbol by applying a transform to it.

 

The symbol geometry is generally close to the origin.

 

Every instance needs a different transformation, depending on where the instance is placed in the model.

  

You need to apply the family instance transformation to the geometry to transform it from the family symbol definition coordinates to the project model coordinates where the instance has been placed.

  

https://www.revitapidocs.com/2020/50aa275d-031e-ce19-9cfd-18a7a341ed19.htm

 

https://www.revitapidocs.com/2020/8c8aff2b-5ff9-e43a-3b5c-308cd0174f1f.htm

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 3 of 4
Anonymous
in reply to: jeremytammik


@jeremytammik wrote:

The family instance reuses the geometry defined for the family symbol by applying a transform to it.

 

The symbol geometry is generally close to the origin.

 

Every instance needs a different transformation, depending on where the instance is placed in the model.

  

You need to apply the family instance transformation to the geometry to transform it from the family symbol definition coordinates to the project model coordinates where the instance has been placed.

  

https://www.revitapidocs.com/2020/50aa275d-031e-ce19-9cfd-18a7a341ed19.htm

 

https://www.revitapidocs.com/2020/8c8aff2b-5ff9-e43a-3b5c-308cd0174f1f.htm

 


Awesome! This worked! Thank you much Jeremy!

 

For anyone interested, here is how I edited the code based on Jeremy's suggestion. ( Still needs to be cleaned up and built upon )

 

    public class Command : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIApplication uiApp = commandData.Application;
            UIDocument uiDoc = uiApp.ActiveUIDocument;
            Document currentDoc = uiDoc.Document;
            Autodesk.Revit.DB.View currentView = uiDoc.ActiveView;
            Selection sel = uiApp.ActiveUIDocument.Selection;
            Transaction transaction = new Transaction(currentDoc);

            Element stringFillType = new FilteredElementCollector(currentDoc)
                .OfClass(typeof(FilledRegionType))
                .FirstOrDefault(x => x.Name == "String 1");
            
            transaction.Start("String fill");

            Reference moduleFaceReference = sel.PickObject(ObjectType.Face);
            GeometryObject geoObj = currentDoc.GetElement(moduleFaceReference).GetGeometryObjectFromReference(moduleFaceReference);
            Instance moduleInstance = currentDoc.GetElement(moduleFaceReference) as Instance;
            Transform moduleTransform = moduleInstance.GetTotalTransform();            
            PlanarFace moduleFace = geoObj as PlanarFace;
            IList<CurveLoop> faceEdges = moduleFace.GetEdgesAsCurveLoops(); 
            
            foreach (CurveLoop loop in faceEdges)
            {
                loop.Transform(moduleTransform);
            }

            FilledRegion fillRegion = FilledRegion.Create(currentDoc, stringFillType.Id, currentView.Id, faceEdges);            
            transaction.Commit();

            Result result = Result.Succeeded;
            return result;

            // throw new NotImplementedException();
        }
    }
Message 4 of 4
jeremytammik
in reply to: Anonymous

Well done!

 

Thank you for the appreciation and glad that it helped!

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Customer Advisory Groups


Rail Community