This document is archived and information here might be outdated.  Recommended version.


How to return CAD transformation settings (ArcObjects .NET 10.5 SDK)

How to return CAD transformation settings


Summary
This topic shows how to return the transformation settings of a computer-aided design (CAD) file and write their properties to the console. This topic uses the Parcels.DWG file (installed with the ArcGIS tutorial); however, the sample code will work with any CAD file.

Returning CAD transformation settings

To return transformation settings to a CAD file, follow these steps:
  1. Instantiate a CAD Workspace Factory.
  2. Get the file workspace by accessing the directory that contains the CAD file.
  3. Cast the IWorkspace interface to IFeatureWorkspace.
  4. Get the feature classes present in the CAD file. In this example, the Point feature class is obtained from IFeatureWorkspace.
  5. Cast the IFeatureLayer interface to the ICadTransformations interface.
  6. Set the transformation mode using the TransformMode property. In this example, the mode is set to esriCadTransformByWorldFile.
  7. Set the world file name for the CadLayer. World files are used to apply a common coordinate system and a common transformation to CAD files.
See the following code:
[C#]
// Set variables for the path and filename.
String nameOfPath="C:\\data\\CAD";
String nameOfCADFile="PARCELS.DWG";
//Instantiate a CADWorkspaceFactory.
ESRI.ArcGIS.Geodatabase.IWorkspaceFactory pWorkspaceFact=new
    ESRI.ArcGIS.DataSourcesFile.CadWorkspaceFactory();
//Get the file's workspace.
ESRI.ArcGIS.Geodatabase.IWorkspace pWorkspace=pWorkspaceFact.OpenFromFile
    (nameOfPath, 0);
//Get the feature's workspace.
ESRI.ArcGIS.Geodatabase.IFeatureWorkspace pFeatureWorkspace;
pFeatureWorkspace=(ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)pWorkspace;
//Open the Feature Class interface.
ESRI.ArcGIS.Geodatabase.IFeatureClass pFeatClass =
    pFeatureWorkspace.OpenFeatureClass(System.String.Concat(nameOfCADFile, ":Point"))
    ;
//Get the CAD Feature Layer interface.
ESRI.ArcGIS.Carto.IFeatureLayer pFeatLayer=new ESRI.ArcGIS.Carto.CadFeatureLayer()
    as ESRI.ArcGIS.Carto.IFeatureLayer;
pFeatLayer.FeatureClass=pFeatClass;
//Get the CadTransformations interface.
ESRI.ArcGIS.DataSourcesFile.ICadTransformations pCadTrans=
    (ESRI.ArcGIS.DataSourcesFile.ICadTransformations)pFeatLayer;
//Enable transformations for CadLayer and print result.
pCadTrans.EnableTransformations=true;
Console.WriteLine("EnableTransformations={0}", pCadTrans.EnableTransformations);
//Set the transform mode for CadLayer and print result.
pCadTrans.TransformMode =
    ESRI.ArcGIS.DataSourcesFile.esriCadTransform.esriCadTransformByWorldFile;
Console.WriteLine("TransformMode={0}", pCadTrans.TransformMode);
//Set the world file name for CadLayer and print result.
pCadTrans.WorldFileName=System.String.Concat(nameOfPath, "\\Parcels.wld");
Console.WriteLine("WorldFileName={0} ", pCadTrans.WorldFileName);
[VB.NET]
'Set variables for filename and feature class.
Dim nameOfPath As String="c:\\data\\CAD"
Dim nameOfCADFile As String="PARCELS.DWG"
'This sample gets the CAD transformation settings.
'Instantiate a CADWorkspaceFactory.
Dim pWorkspaceFact As ESRI.ArcGIS.Geodatabase.IWorkspaceFactory=New ESRI.ArcGIS.DataSourcesFile.CadWorkspaceFactory()
'Get the file's workspace.
Dim pWorkspace As ESRI.ArcGIS.Geodatabase.IWorkspace=pWorkspaceFact.OpenFromFile(nameOfPath, 0)
'Get the feature's workspace.
Dim pFeatureWorkspace As ESRI.ArcGIS.Geodatabase.IFeatureWorkspace
pFeatureWorkspace=DirectCast(pWorkspace, ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)
'Open the Feature Class interface.
Dim pFeatClass As ESRI.ArcGIS.Geodatabase.IFeatureClass=pFeatureWorkspace.OpenFeatureClass(System.String.Concat(nameOfCADFile, ":Point"))
'Get the CAD Feature Layer interface.
Dim pFeatLayer As ESRI.ArcGIS.Carto.IFeatureLayer=TryCast(New ESRI.ArcGIS.Carto.CadFeatureLayer(), ESRI.ArcGIS.Carto.IFeatureLayer)
pFeatLayer.FeatureClass=pFeatClass
'Get the CadTransformations interface.
Dim pCadTrans As ESRI.ArcGIS.DataSourcesFile.ICadTransformations=DirectCast(pFeatLayer, ESRI.ArcGIS.DataSourcesFile.ICadTransformations)
'Enable transformations for CadLayer and print result.
pCadTrans.EnableTransformations=True
Console.WriteLine("EnableTransformations={0}", pCadTrans.EnableTransformations)
'Set the transform mode for CadLayer and print result.
pCadTrans.TransformMode=ESRI.ArcGIS.DataSourcesFile.esriCadTransform.esriCadTransformByWorldFile
Console.WriteLine("TransformMode={0}", pCadTrans.TransformMode)
'Set the world file name for CadLayer and print result.
pCadTrans.WorldFileName="c:\\data\\Parcels.wld"
Console.WriteLine("WorldFileName={0} ", pCadTrans.WorldFileName)






To use the code in this topic, reference the following assemblies in your Visual Studio project. In the code files, you will need using (C#) or Imports (VB .NET) directives for the corresponding namespaces (given in parenthesis below if different from the assembly name):
Development licensing Deployment licensing
ArcGIS Desktop Basic ArcGIS Desktop Basic
ArcGIS Desktop Standard ArcGIS Desktop Standard
ArcGIS Desktop Advanced ArcGIS Desktop Advanced