Active Member
Posts: 7
Registered: ‎05-30-2008
Message 1 of 7 (556 Views)
Accepted Solution


556 Views, 6 Replies
12-15-2010 06:10 PM

Hello All


Does anyone know if the functionallity used in the Command DGNImport is customisable via the API?


I've had a look but I can't seem to find it, but I know there are people out there with much more familiarity with it than me.





Distinguished Contributor
Posts: 112
Registered: ‎04-22-2009
Message 2 of 7 (543 Views)

Re: DGNImport

12-16-2010 12:25 AM in reply to: jpkl

Hi there,


I think your problem will besolved with net next block of code.


        static public void DgnAttach()
            Database db = Application.DocumentManager.MdiActiveDocument.Database;
            using (Transaction trans = Application.DocumentManager.MdiActiveDocument.TransactionManager.StartTransaction())
                DBDictionary nod = (DBDictionary)trans.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForWrite);
                string defDictKey = UnderlayDefinition.GetDictionaryKey(typeof(DgnDefinition));
                if (!nod.Contains(defDictKey))                
                    using (DBDictionary dict = new DBDictionary())                    
                        nod.SetAt(defDictKey, dict);                        
                        trans.AddNewlyCreatedDBObject(dict, true);                    
                ObjectId idDgnDef;                
                DBDictionary dgnDict = (DBDictionary)trans.GetObject(nod.GetAt(defDictKey), OpenMode.ForWrite);                
                using (DgnDefinition dgnDef = new DgnDefinition())                
                    dgnDef.SourceFileName = @"c:\Temp\yourdgnfile.dgn";                    
                    idDgnDef = dgnDict.SetAt("YourDgnFile", dgnDef);                    
                    trans.AddNewlyCreatedDBObject(dgnDef, true);                
                BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);                
                BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);                
                using (DgnReference dgn = new DgnReference())
                    dgn.DefinitionId = idDgnDef;                    
                    trans.AddNewlyCreatedDBObject(dgn, true);


If you want to attach a dwf file the above code can be used. You just need to change the DgnDefinition to DwfDefinition ect.


I hope this helps.


Kind regards,



Active Member
Posts: 7
Registered: ‎05-30-2008
Message 3 of 7 (507 Views)

Re: DGNImport

12-16-2010 03:39 PM in reply to: Irvin



Thank you for your reply.


However what I really need to do is to replicate some of the functionallity of the DGNIMPORT command not the DGNUNDERLAY (for which your code works perfectly for).


That is I need to translate the entities from the DGN file into native AutoCAD entities.


But once again thank you for your time.



New Member
Posts: 1
Registered: ‎04-25-2013
Message 4 of 7 (269 Views)

Re: DGNImport

04-25-2013 11:26 PM in reply to: jpkl

Hi Jason,


I know I am replying to old thread. But have you got any success about translating the DGN entities to DWG?


I am working on the same thing, so your inputs are greatly appreciated.




Active Member
Posts: 7
Registered: ‎05-30-2008
Message 5 of 7 (247 Views)

Re: DGNImport

04-28-2013 09:05 PM in reply to: sandeepkavade

Hi Sandeep


Unfortunately, no I did not have any success in this area.


Bentley are able to read in AutoCAD dwg format very well because of Autodesk's RealDWG. However I don't think they have anything similar to offer and AutoCAD's implementation of this doesn't seem to be public.





Valued Contributor
Posts: 71
Registered: ‎09-26-2007
Message 6 of 7 (230 Views)

Re: DGNImport

04-30-2013 09:07 AM in reply to: jpkl

this will work. 



Each file As System.IO.FileInfo Infiles


TryDim sfilename AsString= file.FullName


Dim sfilenamedwg AsString = mypath & System.IO.Path.GetFileNameWithoutExtension(sfilename) & ".dwg"If System.IO.File.Exists(sfilenamedwg) = TrueThenSystem.IO.File.Delete(sfilenamedwg)


"-dgnimport " & sfilename & vbCr & vbCr & vbCr & vbCr, False, False, True)


"saveas" & vbCr & vbCr & sfilenamedwg & vbCr, False, False, True)


Catch ex AsException



Posts: 13
Registered: ‎12-06-2012
Message 7 of 7 (164 Views)

Re: DGNImport

07-29-2013 11:31 PM in reply to: michael.vanhoose
Yes michael.vanhoose code really works fine

Thanks man
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.