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

Create Structural Columns from CAD dwg file linked into revit (Import Instance)

0 REPLIES 0
Reply
Message 1 of 1
ahmedkhaled96969090
330 Views, 0 Replies

Create Structural Columns from CAD dwg file linked into revit (Import Instance)

  • Good morning,

 

I'm trying to automate the creation of structural elements in Revit using API. I started by linking the dwg file into the rvt model instead of importing it and tried for a start to create columns firstly. After using the Revit Lookup Plugin, I understood how to retrieve Layers information directly from the CAD file to be used in my Method but I'm still stuck as my code gives errors and after debugging the code, I found that some of the elements are null.

 

I intend to:

 

I have attached a copy of the code below for anyone who would like to revise it.

 

  • Get Structural Columns Boundaries from CAD File (Polylines Layers) to transform it into elements in revit.

 

public void FilterGeometryObjects()
    {
        UIApplication uiapp = this.Application;
        
        UIDocument uidoc = uiapp.ActiveUIDocument;
        
        Document doc = uidoc.Document;
        
        Autodesk.Revit.DB.View activeView = doc.ActiveView;
        
        Autodesk.Revit.DB.Options geoOption = uiapp.Application.Create.NewGeometryOptions();
        
        if (null != geoOption)
        {
            geoOption.ComputeReferences = true;
            
            geoOption.View = activeView;
        }
        
        // Geting The CAD File Layers (Only The CAD File Visible In The Active View):
   
        FilteredElementCollector notElemTypeCtor = (new FilteredElementCollector(doc,activeView.Id)).WhereElementIsNotElementType().OfClass(typeof(ImportInstance));

        StringBuilder sb = new StringBuilder();
        
        foreach (Element elem in notElemTypeCtor)
        {
            // Get geometry element of the selected element
            
            GeometryElement geoElem = elem.get_Geometry(geoOption);
            
            // Get geometry object
            
            foreach (GeometryObject geoObj in geoElem)
            {
                // Get the geometry instance which contains the geometry information
                
                GeometryInstance geoInst = geoObj as GeometryInstance;
                
                GeometryElement symbolGeo = geoInst.GetSymbolGeometry();
                
                foreach (GeometryObject gObj in symbolGeo)
                {
                    if ( gObj is Line || gObj is PolyLine || gObj is Arc)                               
                    {
                        GraphicsStyle gStyle = doc.GetElement(gObj.GraphicsStyleId) as GraphicsStyle;
                        
                        var cadLayersIds = gObj.GraphicsStyleId.ToString();
                        
                        var cadLayersNames = gStyle.GraphicsStyleCategory.Name;
                        
                        sb.AppendLine(cadLayersIds + "\t" + cadLayersNames);
                    }
                             
                }
                        
            }

        }
        
        TaskDialog.Show("Revit", sb.ToString());    

    }


    public void CreateColumnsFromCad()
    {
        UIApplication uiapp = this.Application;
        
        UIDocument uidoc = uiapp.ActiveUIDocument;
        
        Document doc = uidoc.Document;
        
        Autodesk.Revit.DB.View activeView = doc.ActiveView;
        
        Autodesk.Revit.DB.Options geoOption = uiapp.Application.Create.NewGeometryOptions();
        
        if (null != geoOption)
        {
            geoOption.ComputeReferences = true;
            
            geoOption.View = activeView;
        }
        
        // Geting The CAD File Layers (Only The CAD File Visible In The Active View):
   
        FilteredElementCollector notElemTypeCtor = (new FilteredElementCollector(doc,activeView.Id)).WhereElementIsNotElementType().OfClass(typeof(ImportInstance));
        
        // For Creation Purpose Only:

        FilteredElementCollector collector = new FilteredElementCollector(doc);        

        // Column Family Symbol:
        
        FamilySymbol columnType = collector.OfClass(typeof(FamilySymbol)).OfCategory(BuiltInCategory.OST_StructuralColumns).FirstElement() as FamilySymbol;
        
        // Str Type:
        
        Autodesk.Revit.DB.Structure.StructuralType structuralType = Autodesk.Revit.DB.Structure.StructuralType.Column;

        // Intended Level:
        
        Level fourthlev;

        fourthlev = collector.OfClass(typeof(Level)).Where(e => e.Name.Equals("FOURTH T.O.C"))  as Level;        // look here
        
        foreach (Element elem in notElemTypeCtor)
        {
            // Get geometry element of the selected element
            
            GeometryElement geoElem = elem.get_Geometry(geoOption);
            
            // Get geometry object
            
            foreach (GeometryObject geoObj in geoElem)
            {
                // Get the geometry instance which contains the geometry information
                
                GeometryInstance geoInst = geoObj as GeometryInstance;
                
                GeometryElement symbolGeo = geoInst.GetSymbolGeometry();
                
                foreach (GeometryObject gObj in symbolGeo)
                {
                    if ( gObj is Line || gObj is PolyLine || gObj is Arc)                               // after debugging, it is found that this condition is skipped while proccessing.
                    {
                        GraphicsStyle gStyle = doc.GetElement(gObj.GraphicsStyleId) as GraphicsStyle;
                        
                        var cadLayersIds = gObj.GraphicsStyleId.ToString();
                        
                        var cadLayersNames = gStyle.GraphicsStyleCategory.Name;

                        List<PolyLine> polyLine = new List<PolyLine>();
                        
                        PolyLine allCadPolys = gObj as PolyLine;
                        
                        polyLine.Add(allCadPolys);
                        
                        IList<XYZ> ptsList;
                        
                        XYZ point;

                        // Trying to Create From a Specific CAD Layer (STR Column Layer in CAD File):
                
                        var colPolys = polyLine.Where(c => c.GraphicsStyleId.Equals(426470));

                        foreach (PolyLine poly in colPolys) 
                        {
                            ptsList = poly.GetCoordinates();
                            
                            foreach (XYZ pt in ptsList) 
                            {
                                point = new XYZ(pt.X, pt.Y, 0);
                                
                                Transaction tns = new Transaction(doc, "Create Columns");
                        
                                using (tns) 
                                {
                                    if (!columnType.IsActive)            // coltype is not active
                                    {
                                        columnType.Activate();
                                        
                                        tns.Start();
                                    }
                            
                                        FamilyInstance column = doc.Create.NewFamilyInstance(point, columnType, fourthlev, structuralType);
                            
                                        tns.Commit();
                                }
                        
                            }

                        }

                    }

                }   
                
            }
       
        }
        
}

 

0 REPLIES 0

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

Post to forums  

Forma Design Contest


Technology Administrators