Setting the points of a two-point CAD transformation
To set the points of a two-point CAD transformation, follow these steps:
-
Instantiate a CAD Workspace Factory.
-
Get the file workspace by accessing the directory that contains the CAD file.
-
Cast the IWorkspace interface to IFeatureWorkspace.
-
Get the feature classes present in the CAD file. In this example, the Polyline feature class is obtained from IFeatureWorkspace.
-
Cast the IFeatureLayer interface to the ICadTransformations interface.
-
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.
-
Set the transformation points using the SetFromToTransform method.
-
Use the GetFromToTransform method to get the current from and to points of the CAD layer.
-
Control points define the source and destination coordinates, and this example can now be used to georeference a CAD dataset.
See the following code:
[C#] // Set variables for the path and filename.
String nameOfPath="C:\\data\\CAD";
String nameOfCADFile="PARCELS.DWG";
//Set the Workspace Factory.
ESRI.ArcGIS.Geodatabase.IWorkspaceFactory pWorkspaceFact=new
ESRI.ArcGIS.DataSourcesFile.CadWorkspaceFactory();
//Open the workspace.
ESRI.ArcGIS.Geodatabase.IWorkspace pWorkspace=pWorkspaceFact.OpenFromFile
(nameOfPath, 0);
//Set the feature workspace.
ESRI.ArcGIS.Geodatabase.IFeatureWorkspace pFeatureWorkspace=
(ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)pWorkspace;
//Open the Feature Class.
ESRI.ArcGIS.Geodatabase.IFeatureClass pFeatClass =
pFeatureWorkspace.OpenFeatureClass(System.String.Concat(nameOfCADFile,
":Polyline"));
//Set the Feature Layer to the Feature Class.
ESRI.ArcGIS.Carto.IFeatureLayer pFeatLayer=new ESRI.ArcGIS.Carto.CadFeatureLayer()
as ESRI.ArcGIS.Carto.IFeatureLayer;
pFeatLayer.FeatureClass=pFeatClass;
//Set the CadTransformations.
ESRI.ArcGIS.DataSourcesFile.ICadTransformations pCadTrans=
(ESRI.ArcGIS.DataSourcesFile.ICadTransformations)pFeatLayer;
//Set the WKSPoints.
ESRI.ArcGIS.esriSystem.WKSPoint[] pWks1=new ESRI.ArcGIS.esriSystem.WKSPoint[2];
ESRI.ArcGIS.esriSystem.WKSPoint[] pWks2=new ESRI.ArcGIS.esriSystem.WKSPoint[2];
//Store the transformation points.
pWks1[0].X=1222999.95;
pWks1[0].Y=177999.94;
pWks2[0].X=1231999.96;
pWks2[0].Y=184000.01;
pWks1[1].X=2222999.95;
pWks1[1].Y=277999.94;
pWks2[1].X=2231999.96;
pWks2[1].Y=284000.01;
//Set the from and to points for a two-point transformation.
pCadTrans.SetFromToTransform(ref pWks1[0], ref pWks2[0], ref pWks1[1], ref pWks2[1]);
//Get the from and to points of the two-point transformation of the CadLayer.
pCadTrans.GetFromToTransform(out pWks1[0], out pWks2[0], out pWks1[1], out pWks2[1]);
Console.WriteLine("GetFromToTransform (From1 x,y)={0},{1}", pWks1[0].X, pWks1[0].Y)
;
Console.WriteLine("GetFromToTransform (From2 x,y)={0},{1}", pWks2[0].X, pWks2[0].Y)
;
Console.WriteLine("GetFromToTransform (To1 x,y)={0},{1}", pWks1[1].X, pWks1[1].Y);
Console.WriteLine("GetFromToTransform (To2 x,y)={0},{1}", pWks2[1].X, pWks2[1].Y);
[VB.NET] ' Set variables for filename and feature class.
Dim nameOfPath As String="C:\\arcgis\\ArcTutor\\Editor\\ExerciseData\\EditingCAD"
Dim nameOfCADFile As String="PARCELS.DWG"
'Set the Workspace Factory.
Dim pWorkspaceFact As ESRI.ArcGIS.Geodatabase.IWorkspaceFactory=New ESRI.ArcGIS.DataSourcesFile.CadWorkspaceFactory()
'Open the workspace.
Dim pWorkspace As ESRI.ArcGIS.Geodatabase.IWorkspace=pWorkspaceFact.OpenFromFile(nameOfPath, 0)
'Set the feature workspace.
Dim pFeatureWorkspace As ESRI.ArcGIS.Geodatabase.IFeatureWorkspace=DirectCast(pWorkspace, ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)
'Open the Feature Class.
Dim pFeatClass As ESRI.ArcGIS.Geodatabase.IFeatureClass=pFeatureWorkspace.OpenFeatureClass(System.String.Concat(nameOfCADFile, ":Polyline"))
'Set the Feature Layer to the Feature Class.
Dim pFeatLayer As ESRI.ArcGIS.Carto.IFeatureLayer=TryCast(New ESRI.ArcGIS.Carto.CadFeatureLayer(), ESRI.ArcGIS.Carto.IFeatureLayer)
pFeatLayer.FeatureClass=pFeatClass
'Set the CadTransformations.
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(1) {}
Dim pWks2 As ESRI.ArcGIS.esriSystem.WKSPoint()=New ESRI.ArcGIS.esriSystem.WKSPoint(1) {}
'Store the transformation points.
pWks1(0).X=1222999.95
pWks1(0).Y=177999.94
pWks2(0).X=1231999.96
pWks2(0).Y=184000.01
pWks1(1).X=2222999.95
pWks1(1).Y=277999.94
pWks2(1).X=2231999.96
pWks2(1).Y=284000.01
'Set the from and to points for a two-point transformation.
pCadTrans.SetFromToTransform(pWks1(0), pWks2(0), pWks1(1), pWks2(1))
'Get the from and to points of the two-point transformation of the CadLayer.
pCadTrans.GetFromToTransform(pWks1(0), pWks2(0), pWks1(1), pWks2(1))
Console.WriteLine("GetFromToTransform (From1 x,y)={0},{1}", pWks1(0).X, pWks1(0).Y)
Console.WriteLine("GetFromToTransform (From2 x,y)={0},{1}", pWks2(0).X, pWks2(0).Y)
Console.WriteLine("GetFromToTransform (To1 x,y)={0},{1}", pWks1(1).X, pWks1(1).Y)
Console.WriteLine("GetFromToTransform (To2 x,y)={0},{1}", pWks2(1).X, pWks2(1).Y)
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):
- ESRI.ArcGIS.System (ESRI.ArcGIS.esriSystem)
- ESRI.ArcGIS.Version (ESRI.ArcGIS)
- ESRI.ArcGIS.Carto
- ESRI.ArcGIS.DataSourcesFile
- ESRI.ArcGIS.Display
- ESRI.ArcGIS.Geodatabase
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 |