Several ways to open a CAD file

Source: Internet
Author: User
Tags polyline

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.