How to set CAD transformation properties (from, to, angle, and scale)


Summary
This topic shows how to set the rotation, scale, and translation computer-aided design (CAD) transformation properties and write the 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.

Setting CAD transformation properties

To set the CAD transformation properties (from, to, angle, and scale), 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. Instantiate four WKS points (which are two-dimensional points). In this example, pWks1 represents the two "from" points and pWks2 represents the two "to" points.
  7. Set two double variables representing angle and scale.
  8. Use the SetTransform method (containing the from point, to point, angle, and scale parameters) to set the transformation. This transforms the CAD layer accordingly.
  9. Use the GetTransform method to get the current transformation settings of the CAD layer.
  10. Rotate and set the scale of the desired CAD layer.
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;
//Set the WKSPoints.
ESRI.ArcGIS.esriSystem.WKSPoint[] pWks1 = new ESRI.ArcGIS.esriSystem.WKSPoint[1];
ESRI.ArcGIS.esriSystem.WKSPoint[] pWks2 = new ESRI.ArcGIS.esriSystem.WKSPoint[1];
//Store the transformation values.
pWks1[0].X = 1222999.95;
pWks1[0].Y = 177999.94;
pWks2[0].X = 2222999.95;
pWks2[0].Y = 277999.94;
double dblAngle = 90;
double dblScale = 2;
//Set rotation, scale, and translation.
pCadTrans.SetTransformation(ref pWks1[0], ref pWks2[0], dblAngle, dblScale);
//Get and print rotation, scale, and translation values.
pCadTrans.GetTransformation(out pWks1[0], out pWks2[0], out dblAngle, out dblScale);
Console.WriteLine("GetTransformation(from x,y) = {0},{1}", pWks1[0].X, pWks1[0].Y);
Console.WriteLine("GetTransformation(to x,y)    = {0},{1}", pWks2[0].X, pWks2[0].Y);
Console.WriteLine("GetTransformation(angle) = {0}", dblAngle);
Console.WriteLine("GetTransformation(scale) = {0}", dblScale);
[VB.NET]
'Set variables for filename and feature class.
Dim nameOfPath As String = "C:\\data\\CAD"
Dim nameOfCADFile As String = "PARCELS.DWG"
'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)
'Set the WKSPoints.
Dim pWks1 As ESRI.ArcGIS.esriSystem.WKSPoint() = New ESRI.ArcGIS.esriSystem.WKSPoint(0) {}
Dim pWks2 As ESRI.ArcGIS.esriSystem.WKSPoint() = New ESRI.ArcGIS.esriSystem.WKSPoint(0) {}
'Store the transformation values.
pWks1(0).X = 1222999.95
pWks1(0).Y = 177999.94
pWks2(0).X = 2222999.95
pWks2(0).Y = 277999.94
Dim dblAngle As Double = 90
Dim dblScale As Double = 2
'Set rotation, scale, and translation.
pCadTrans.SetTransformation(pWks1(0), pWks2(0), dblAngle, dblScale)
'Get and print rotation, scale, and translation values.
pCadTrans.GetTransformation(pWks1(0), pWks2(0), dblAngle, dblScale)
Console.WriteLine("GetTransformation(from x,y) = {0},{1}", pWks1(0).X, pWks1(0).Y)
Console.WriteLine("GetTransformation(to x,y) = {0},{1}", pWks2(0).X, pWks2(0).Y)
Console.WriteLine("GetTransformation(angle) = {0}", dblAngle)
Console.WriteLine("GetTransformation(scale) = {0}", dblScale)






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 for Desktop Basic ArcGIS for Desktop Basic
ArcGIS for Desktop Standard ArcGIS for Desktop Standard
ArcGIS for Desktop Advanced ArcGIS for Desktop Advanced