several ways to open CAD files from the original text
Iworkspacefactory pworkspacefactory;
Ifeatureworkspace Pfeatureworkspace;
Ifeaturelayer Pfeaturelayer;
Ifeaturedataset Pfeaturedataset;
Open a CAD data set
Pworkspacefactory = new Cadworkspacefactoryclass ();
Pfeatureworkspace = (ifeatureworkspace) pworkspacefactory.openfromfile (directorypath, 0);
Open a feature set
Pfeaturedataset = Pfeatureworkspace.openfeaturedataset (fileName);
Ifeaturclasscontainer can manage each feature class in the Ifeaturedataset
Ifeatureclasscontainer Pfeatureclasscontainer = (ifeatureclasscontainer) pfeaturedataset;
Traversal of features in a CAD file
for (int i = 0; i < pfeatureclasscontainer.classcount-1; i++)
{
Ifeatureclass Pfeatureclass = Pfeatureclasscontainer.get_class (i);
if (Pfeatureclass.featuretype = = esrifeaturetype.esriftannotation)
{
If it is annotation, add the annotation layer
Pfeaturelayer = new Cadannotationlayerclass ();
}
else//Add a feature layer if it is a point, line, or polygon
{
Pfeaturelayer = new Featurelayerclass ();
Pfeaturelayer.name = Pfeatureclass.aliasname;
Pfeaturelayer.featureclass = Pfeatureclass;
This.axmc_Main.Map.AddLayer (Pfeaturelayer);
this.axmc_main.activeview.** ();
}
}
Method Two, directly open the CAD data point line surface annotation and other feature classes, the loaded data are a separate layer.
The specific code is as follows:
Cadworkspacefactoryclass fac=new Cadworkspacefactoryclass ();
String filepath= "E:\CAD ";
Ifeatureworkspace SPACE=FAC. Openfromfile (filepath,0) as Ifeatureworkspace;
Here's the code to open Featureclass:
Line
Ifeatureclass polyline =space. Openfeatureclass ("NewCAD.dwg:Polyline");
Ifeaturelayer layer=new Cadfeaturelayerclass ();
Layer. Featureclass =polyline;
Point
Ifeatureclass Point=space. Openfeatureclass ("NewCAD.dwg:Point");
Layer=new Cadfeaturelayerclass ();
Layer. Featureclass =point;
Surface
Ifeatureclass Polygon=space. Openfeatureclass ("NewCAD.dwg:Polygon");
Ayer=new Cadfeaturelayerclass ();
Layer. Featureclass =polygon;
Notes
Ifeatureclass Anno=space. Openfeatureclass ("NewCAD.dwg:Annotation");
Layer=new Cadannotationlayerclass ();
Layer. Featureclass =anno;
In fact, the coverage file is developed in a manner similar to the AutoCAD method, which can be consulted.
Method three, using the drawing space to directly load CAD data set, after loading each type of data as a layer display.
Iworkspacefactory pworkspacefactory = new Cadworkspacefactoryclass ();
Iworkspace pworkspace = pworkspacefactory.openfromfile (directorypath, 0);
--Define a CAD drawing space and assign it to the open workspace above
Icaddrawingworkspace pcaddrawingworkspace = pworkspace as Icaddrawingworkspace;
--Define a CAD drawing dataset and open a CAD diagram in the workspace specified above
--then assign a value to the CAD data set
Icaddrawingdataset Pcaddrawingdataset = Pcaddrawingworkspace.opencaddrawingdataset (fileName);
--Through the Icadlayer class, the above-obtained CAD Data Bureau assigns the value to the Icadlayer class object
--caddrawingdataset Property
Icadlayer Pcadlayer = new Cadlayerclass ();
Pcadlayer.caddrawingdataset = Pcaddrawingdataset;
--load CAD layer with Mapcontrol
This.axmc_Main.Map.AddLayer (Pcadlayer);
this.axmc_main.activeview.** ();
Method four, but also traverse the CAD point line surface, but do not check whether the data type is annotation.
Iworkspacefactory pworkspacefactory = new Cadworkspacefactoryclass ();
Ifeatureworkspace pfeatureworkspace = pworkspacefactory.openfromfile (directorypath, 0) as IFeatureWorkspace;
Ifeaturedataset Pfeaturedataset = Pfeatureworkspace.openfeaturedataset (fileName);
Ifeatureclasscontainer Pfeatureclasscontainer = (ifeatureclasscontainer) pfeaturedataset;
for (int i = 0; i < pfeatureclasscontainer.classcount-1; i++)
{
Ifeatureclass Pfeatureclass = Pfeatureclasscontainer.get_class (i);
Ifeaturelayer Pfeaturelayer = new Featurelayerclass ();
Pfeaturelayer.featureclass = Pfeatureclass;
Icadlayer Pcadlayer = new Cadlayerclass ();
Pcadlayer = Pfeaturelayer as Icadlayer;
This.axmc_Main.Map.AddLayer (Pfeaturelayer);
this.axmc_main.activeview.** ();
}
Method Five, similar and method three.
Add passed file to Mapcontrol
Icaddrawingdataset Pcaddrawingdataset = Getcaddrawingdataset (DirectoryPath, fileName);
if (Pcaddrawingdataset = = null) return;
Icadlayer Pcadlayer = new Cadlayerclass ();
Pcadlayer.caddrawingdataset = Pcaddrawingdataset;
Pcadlayer.name = FileName;
This.axmc_Main.AddLayer (pcadlayer, 0);
<summary>
Get record set
</summary>
<param name= "DirectoryPath" ></param>
<param name= "FileName" ></param>
<returns></returns>
Private Icaddrawingdataset Getcaddrawingdataset (String directorypath, String fileName)
{
Create a WorkspaceName Object
Iworkspacename pworkspacename = new Workspacenameclass ();
Pworkspacename.workspacefactoryprogid = "Esridatasourcesfile.cadworkspacefactory";
Pworkspacename.pathname = DirectoryPath;
Create a Caddrawingname object
Idatasetname pdatasetname = new Caddrawingnameclass ();
Pdatasetname.name = FileName;
Pdatasetname.workspacename = Pworkspacename;
Open the CAD drawing
Iname pName = (iname) pdatasetname;
Return (Icaddrawingdataset) Pname.open ();
}
Several ways to open a CAD file