AutoCAD Development based on Visual C #--Example 1 Basic example

Source: Internet
Author: User

Example 1 Basic examples

1 , new Project

Start Visual Studio 2005, create a new project named CAD development, as shown in the following illustration:

2 , adding references

Add AutoCAD references AutoCAD 2007 type library and autocad/objectdbx Common 17.0 type library, as shown in the following illustration:

3 , global variable definition

Defined in the global variable as follows:

Autodesk.AutoCAD.Interop.AcadApplication Acadapp;

Autodesk.AutoCAD.Interop.AcadDocument Acaddoc;

Among them, Acadapplication is CAD object interface, Acaddocument is CAD file.

4 , open CAD file

Add a button to add the following code for the button to enable the ability to open a CAD file:

private void Button1_Click (object sender, EventArgs e)

{

OpenFileDialog POPENDG = new OpenFileDialog ();

Popendg.showdialog ();

string filePath = Popendg.filename;

Acadapp = new Acadapplication ();

AcadApp.Application.Visible = true;

Acaddoc = AcadApp.Documents.Open (filepath,null,null);

}

The results of the program run as shown in the following illustration:

If there is no statement "AcadApp.Application.Visible = true;" The CAD software interface cannot be displayed, only CAD can be started in the background. Through "Acadapp = new Acadapplication ();" is to create a new CAD object, but not every time you need to create a CAD object, such as when a CAD object already exists, you do not need to create a CAD object.

5 , define startup CAD the function of the object

Define a function to start CAD objects "start CAD ()" With the following code:

private void startup CAD ()

{

Try

{

Acadapp = (acadapplication) System.Runtime.InteropServices.Marshal.GetActiveObject ("Autocad.application");

Acaddoc = acadapp.activedocument;

}

Catch

{

OpenFileDialog POPENDG = new OpenFileDialog ();

Popendg.filter = "CAD file (*.DWG) |*.dwg| CAD graphic file (*.dxf) |*.dxf ";

Popendg.title = "Open CAD file";

Popendg.showdialog ();

string filePath = Popendg.filename;

if (FilePath = "")

{

MessageBox.Show ("Select the CAD file is invalid.) "," File is invalid. ");

Application.exit ();

}

Acadapp = new Acadapplication ();

Acaddoc = AcadApp.Documents.Open (FilePath, NULL, NULL);

}

AcadApp.Application.Visible = true;

Microsoft.VisualBasic.Interaction.AppActivate (acadapp.caption);

}

of which "Microsoft.VisualBasic.Interaction.AppActivate (acadapp.caption);" is to make the CAD object at the front, you also need to add the reference "Microsoft.VisualBasic" to call the function, as shown in the following illustration:

Related Article

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.