Methods for opening AutoCAD files and reading CAD data in arcengine

Source: Internet
Author: User
Tags polyline

Method 1,

Iworkspacefactory pworkspacefactory;
Ifeatureworkspace pfeatureworkspace;
Ifeaturelayer pfeaturelayer;
Ifeaturedataset pfeaturedataset;
// Open the CAD Dataset
Pworkspacefactory = new cadworkspacefactoryclass ();
Pfeatureworkspace = (ifeatureworkspace) pworkspacefactory. openfromfile (directorypath, 0 );
// Open a feature set
Pfeaturedataset = pfeatureworkspace. openfeaturedataset (filename );
// Ifeaturclasscontainer can manage each element class in ifeaturedataset
Ifeatureclasscontainer pfeatureclasscontainer = (ifeatureclasscontainer) pfeaturedataset;
// Traverse the elements in the 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 a note, add the note Layer
Pfeaturelayer = new cadannotationlayerclass ();
}
Else // if it is a vertex, line, or surface, add the element layer
{
Pfeaturelayer = new featurelayerclass ();
Pfeaturelayer. Name = pfeatureclass. aliasname;
Pfeaturelayer. featureclass = pfeatureclass;

This. axmc_main.map.addlayer (pfeaturelayer );
This. axmc_main.activeview .**();
}
}

Method 2,

// -- Define the workspace and instantiate it with cadworkspacefactoryclass ()
Iworkspacefactory pworkspacefactory = new cadworkspacefactoryclass ();
// -- Open the corresponding workspace and assign it to the element space. openfromfile ()
// -- The parameter in is the path of the CAD folder
Ifeatureworkspace pfeatureworkspace = pworkspacefactory. openfromfile (directorypath, 0) as ifeatureworkspace;
/* -- Open the line element class. To open the element of the point type, you need to convert the following code:
* -- Ifeatureclass pfeatureclass = pfeatureworkspace. openfeatureclass (filename + ": Point ");
* -- It can be seen that filename is the name of the CAD chart, followed by the type of the element class to be opened, with a colon in the middle
* -- Separated. You can think about how polygon and annotation are opened. */
// Point polyline
Ifeatureclass pfeatureclass = pfeatureworkspace. openfeatureclass (filename + ": Point ");
Ifeaturelayer pfeaturelayer = new featurelayerclass ();
Pfeaturelayer. featureclass = pfeatureclass;
This. axmc_main.map.addlayer (pfeaturelayer );
This. axmc_main.activeview .**();

 

Method 3,

/* -- The following two lines of code define a cad workspace and open it, but this time it is not assigned
-- Ifeatureworkspace object, which is assigned to the iworkspace Defined Object */
Iworkspacefactory pworkspacefactory = new cadworkspacefactoryclass ();
Iworkspace pworkspace = pworkspacefactory. openfromfile (directorypath, 0 );
// -- Define a CAD Drawing space and assign the workspace opened above to it
Icaddrawingworkspace pcaddrawingworkspace = pworkspace as icaddrawingworkspace;
// -- Define a CAD Drawing dataset and open a cad diagram in the specified workspace above
// -- Then assign a value to the CAD Dataset
Icaddrawingdataset pcaddrawingdataset = pcaddrawingworkspace. opencaddrawingdataset (filename );
// -- Assign the CAD data obtained above to
// -- Caddrawingdataset attributes
Icadlayer pcadlayer = new cadlayerclass ();
Pcadlayer. caddrawingdataset = pcaddrawingdataset;
// -- Use mapcontrol to load the CAD Layer
This. axmc_main.map.addlayer (pcadlayer );
This. axmc_main.activeview .**();

Method 4,

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 5,

// 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>
/// Obtain the 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 = "esristmcesfile. 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 ();
}

 

Original article: http://maruigxxg.blog.163.com/blog/static/3051816420106623712567/

 

Because the AutoCAD file is composed of several files, the use of arcengine software to open AutoCAD data is somewhat different from other data, the specific code is as follows:

 

Cadworkspacefactoryclass FAC = new cadworkspacefactoryclass ();
String filepath = "E: \ CAD ";
Ifeatureworkspace Space = FAC. openfromfile (filepath, 0) as ifeatureworkspace;
The following code opens 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;
// Plane
Ifeatureclass polygon = space. openfeatureclass ("newcad. DWG: polygon ");
Ayer = new cadfeaturelayerclass ();
Layer. featureclass = polygon;
// Note
Ifeatureclass Anno = space. openfeatureclass ("newcad. DWG: annotation ");
Layer = new cadannotationlayerclass ();
Layer. featureclass = Anno;
// In fact, the development method of coverage files is similar to that of AutoCAD. For details, refer.

 

Original article: http://www.gissky.net/blog/user1/yumao/16035.html

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.