Add .dwg file to Map using Code

2795
7
04-12-2016 07:36 PM
VeroniqueXie
New Contributor

     One of our customers have thousands of CAD .dwg files. The need an function on the Web that they can provide a .dwg file name, and get an .PDF file.

    Export to PDF  from .mxd or .lyr file is quite easy.  The problem is how to make an .mxd or .lyr file from a .dwg file.

    Of cause I can using ArcMap, add .dwg file using the Add Data Dialog. As I learn from the Desktop Help:

    "When you add CAD data to a map document, features are drawn to match the CAD drawing's original symbology as closely as possible. The default CAD renderer draws CAD features by unique values combining line type, color, and line weight properties."

    And I got a map like this, it's pretty good:

   1.png

  The problem is  we need to automate the process. so we using the flowing code to add the Cad Drawing to the Map:

 

  Set pCadDrawingDataset = GetCadDataset("d:\data\cad", "e-51878.dwg")

    If pCadDrawingDataset Is Nothing Then Exit Sub

    Set pCadLayer = New CadLayer

    Set pCadLayer.CadDrawingDataset = pCadDrawingDataset

    pCadLayer.Name = "e-51878.dwg"

    pMxDoc.FocusMap.AddLayer pCadLayer

It works, but the map looked like this:

   2.png

The line color is OK, but the  Annotation is too big compares the the first image, and the whole dwg file becomes one layer.  Since it is become one layer, we can not modify the display of the annotation.

      Of cause we can add the .dwg layer by layer using  .dwg:polyline, .dwg:polygon.  But this method can not do the automatic symbolization.

So my question is : Is it possible to do the "Add Data to Map"  by the ArcObjects Code? So that I can get the first map. I know there is a IAddDataDialog, but it will popup an dialog.

ArcMap using a special renderer for CAD .dwg file, it is very difficult to do it in Code:

3.png

0 Kudos
7 Replies
VeroniqueXie
New Contributor

       I guest the best way is to add may be 10-20 .dwg files one time in ArcMap using the Add Data,  then save each .dwg group layer  to .lyr file using python code; keep doing it until we create a .lyr file for each .dwg file. Thus the .lyr files will have the renderer information of the .dwg files. Finally we can use AO or Python to export PDF using the exist .lyr file, get the right symbol and annotation.

0 Kudos
VeroniqueXie
New Contributor

    I searched the document and find that we can drop the .dwg file from a Catlog window instead of using the "Add Data" button:

4.png

   This method also creates a group layer, can we automate this process by ArcObjects code? Thanks a lot.

0 Kudos
NeilAyres
MVP Frequent Contributor

On the subject of the annotation changing size.

Does the input CAD file have a known projection? normally this would mean you have a prj file next to the CAD with the same basename.

0 Kudos
VeroniqueXie
New Contributor

Thanks for the reply.

The CAD files do not have any projection files, it is come from other agency who only use the AutoCAD.

0 Kudos
NeilAyres
MVP Frequent Contributor

Well that probably explains why the annotation changes size.

0 Kudos
VeroniqueXie
New Contributor

      Yes, That could be the reason for the annotation. But the annotation works well when adding the .dwg using the "Add Data" button in ArcMap. Seems the "Add Data" using better mechanism to show the CAD data.  That is why we want simulate that process in Code.

0 Kudos
rajeshc
New Contributor II

I was looking for a solution for  exactly the same requirement as described in this thread.  Though it is an old thread, it may help someone.  The solution is given below.

Public Sub AddCADLayer2GroupLayer(pGrpLayer As IGroupLayer, cadFolderPath As String, cadfilename As String)

Dim pFeatureLayer As ESRI.ArcGIS.Carto.IFeatureLayer = Nothing
Dim factory As IWorkspaceFactory = New CadWorkspaceFactory
Dim pFeatureWorkspace As IFeatureWorkspace = CType(factory.OpenFromFile(cadFolderPath, 0), IFeatureWorkspace)
Dim pFeatureDataset As IFeatureDataset = CType(pFeatureWorkspace.OpenFeatureDataset(cadfilename), IFeatureDataset)
Dim pFeatureClassContainer As IFeatureClassContainer = CType(pFeatureDataset, IFeatureClassContainer)

For i As Integer = 0 To pFeatureClassContainer.ClassCount - 1


Dim pFeatureClass As IFeatureClass = pFeatureClassContainer.Class(i)

If pFeatureClass.FeatureType = esriFeatureType.esriFTAnnotation Or pFeatureClass.FeatureType = esriFeatureType.esriFTCoverageAnnotation Then

pFeatureLayer = New CadAnnotationLayerClass
pFeatureLayer.Name = pFeatureClass.AliasName
pFeatureLayer.FeatureClass = pFeatureClass

Else


pFeatureLayer = New CadFeatureLayer
pFeatureLayer.Name = pFeatureClass.AliasName
pFeatureLayer.FeatureClass = pFeatureClass
Dim iglayer As ESRI.ArcGIS.Carto.IGeoFeatureLayer = CType(pFeatureLayer, ESRI.ArcGIS.Carto.IGeoFeatureLayer)

'Here the Renderer is set
Dim pUID As New UID ' create a new UID objects
'progID is "esriCartoUI.CadUniqueValuePropertyPage"
pUID.Value = "esriCartoUI.CadUniqueValuePropertyPage"
iglayer.RendererPropertyPageClassID = pUID

End If

If pFeatureLayer IsNot Nothing Then
pGrpLayer.Add(pFeatureLayer)
End If

Next

End Sub

0 Kudos