Import AutoCAD data to Revit.

Import AutoCAD data to Revit.

aniket_pachore
Enthusiast Enthusiast
574 Views
3 Replies
Message 1 of 4

Import AutoCAD data to Revit.

aniket_pachore
Enthusiast
Enthusiast

I import the AutoCAD Data to Revit. And I store it in different lists for further processing. I am getting the Layer wise lines and Polylines of Doors, Windows, Walls, etc. but I am not able to store the Blocks of particular name from AutoCAD file in Revit. 


Here is my code

ElementId mDwgElemId = null;
List<PolyLine> wallPolyLineList = null;
List<PolyLine> windowPolyLineList = null;
List<PolyLine> doorPolyLineList = null;

private void SelectDWGFile(Autodesk.Revit.DB.Document document)
        {
            bool imported = false;
            DWGImportOptions dwgImportOption = new DWGImportOptions();

            dwgImportOption.ColorMode = Autodesk.Revit.DB.ImportColorMode.Inverted;
            dwgImportOption.CustomScale = 0.0;
            dwgImportOption.Unit = Autodesk.Revit.DB.ImportUnit.Millimeter;
            dwgImportOption.OrientToView = true;
            dwgImportOption.Placement = Autodesk.Revit.DB.ImportPlacement.Origin;
            dwgImportOption.ThisViewOnly = false;

            Autodesk.Revit.DB.View view = document.ActiveView;


            // Prompt the user to select a DWG file using a file dialog
            System.Windows.Forms.OpenFileDialog openFileDialog = new System.Windows.Forms.OpenFileDialog();
            openFileDialog.Filter = "AutoCAD DWG Files|*.dwg";
            openFileDialog.Title = "Select AutoCAD DWG File";

            String m_importFileFullName = null;
            if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string selectedFilePath = openFileDialog.FileName;

                m_importFileFullName = selectedFilePath;

            }

            Transaction transaction = new Transaction(document);
            transaction.SetName("Import");
            transaction.Start();
            imported = document.Import(m_importFileFullName, dwgImportOption, view, out mDwgElementId);
            document.Regenerate();
            mUI_Document.RefreshActiveView();
            transaction.Commit();
        }

private void ImportAutoCADFileData(Autodesk.Revit.DB.Document document, ElementId m_dwgElementId, ref List<PolyLine> wallPolyLineList, ref List<PolyLine> windowPolyLineList, ref List<PolyLine> doorPolyLineList)
        {

            Transaction transaction = new Transaction(document);
            transaction.SetName("ImportData");
            transaction.Start();
            ImportInstance dwg = document.GetElement(m_dwgElementId) as ImportInstance;

            wallPolyLineList = new List<PolyLine>();
            windowPolyLineList = new List<PolyLine>();
            doorPolyLineList = new List<PolyLine>();

            string drawingInstanceName = dwg.Name;

            foreach (GeometryObject geometryObj in dwg.get_Geometry(new Options()))
            {
                if (geometryObj is GeometryInstance)                      
                {
                    GeometryInstance dwgInstance = geometryObj as GeometryInstance;

                    foreach (GeometryObject blockObject in dwgInstance.SymbolGeometry)
                    {
                        GraphicsStyle gStyle = document.GetElement(blockObject.GraphicsStyleId) as GraphicsStyle;
                        string layerName = gStyle.GraphicsStyleCategory.Name;

                        if (layerName == "Wall_Main")
                        {
                            if (blockObject is PolyLine)
                            {
                                PolyLine polyLine = blockObject as PolyLine;
                                wallPolyLineList.Add(polyLine);
                                //floorPolyLineList.Add(polyLine);
                            }
                        }
                        else if (layerName == "Wall_Shop")
                        {
                            if (blockObject is PolyLine)
                            {
                                PolyLine polyLine = blockObject as PolyLine;
                                wallPolyLineList.Add(polyLine);
                            }
                        }
                        else if (layerName == "Stair_FloorCut")
                        {
                            if (blockObject is PolyLine)
                            {
                                PolyLine polyLine = blockObject as PolyLine;
                                wallPolyLineList.Add(polyLine);
                                floorCutPolyLineList.Add(polyLine);
                            }
                        }
                        else if (layerName == "Poarch_Area")
                        {
                            if (blockObject is PolyLine)
                            {
                                PolyLine polyLine = blockObject as PolyLine;
                                wallPolyLineList.Add(polyLine);
                            }
                        }
                        else if (layerName == "Window")
                        {
                            if (blockObject is PolyLine)
                            {
                                PolyLine polyLine = blockObject as PolyLine;
                                windowPolyLineList.Add(polyLine);
                            }
                        }
                        else if (layerName == "Door_House")
                        {
                            if (blockObject is PolyLine)
                            {
                                PolyLine polyLine = blockObject as PolyLine;
                                doorPolyLineList.Add(polyLine);
                            }
                        }
                        else if (blockObject is PolyLine)
                        {
                            PolyLine polyLine = blockObject as PolyLine;
                            wallPolyLineList.Add(polyLine);
                        }
                    }
                }
            }
            transaction.Commit();
        }

 

 

0 Likes
575 Views
3 Replies
Replies (3)
Message 2 of 4

admaecc211151
Advocate
Advocate

Well, I'd like to know these, too.

 

------

I always full explode and delete them later, the blocks also explode.

And the linetypes and texttypes would create as new, I have to check and delete them, either.

------

When partical explode, the blocks become new Import Symbol named <old dwg name>.<block name>

0 Likes
Message 3 of 4

aniket_pachore
Enthusiast
Enthusiast

My aim is to remove the different block names and place a family instance, such as a table or chair, on a specific block. However, I am not able to get the block name when I import the AutoCAD drawing to Revit. Is there any way to get the block name and its position?

Message 4 of 4

TripleM-Dev.net
Advisor
Advisor

Hi @aniket_pachore 

 

If I'm correct "if (geometryObj is GeometryInstance) " are the blocks, the first one would be the whole drawing, all next GeometryInstances would be the blocks.

 

Depending on nesting of the blocks you would be missing deeper nested blocks, it should be build as a recursive call if a GeometryInstance is found (nested function that calls itself if a GeometryInstance if found)

 

0 Likes