Use ObjectDBX technology in C # Never open information to get information in graphics
C # Talent Bird (QQ: 249178521)
Can I get information about the drawing from the unmoping graphic? Answer is affirmative. The specific implementation method will be described below.
Claim:
n uses C # programming
n Read I wrote "Secondary Development of AutoCAD" in C # (Yes in 9CBS)
Start:
N new construction C # console programs in Visual Studio.net
n Add the following class library in the reference tab:
l interop.autocad.dll
l acadexample.dll
l ObjectDBX16 (right-click "Reference" tab in "Solution Explorer", select Add References in the pop-up menu, select in the Add Reference dialog box in the drop-down list box under "COM" tab "AutoCAD / ObjectDbx Common 16.0 Type Library" item)
Then type the following code:
1: USING System;
2: USING AutoCAD;
3: using dbx = axdblib;
4: using acadexample;
5:
6: Namespace ConsoleApplication1
7: {
8: ///
9: /// Summary Description for Class1.
10: /// summary>
11: Class Class1
12: {
13: ///
14: /// The main entry point for the application.
15: /// summary>
16: [Stathread]
17: Static void
Main
(String [] ARGS)
18: {
19: Using (AutoCadconnector Connector = New AutoCadConnector ())
20: {
21: String progid = "objectdbx.axdbdocument.16" // Note, this is the AUTOCAD2004 writing,
// If AutoCAD2002 and AutoCAD2000i are "ObjectDbx.axdbDocument.1"
22: acadapplication acadapp = connector.Application;
23: dbx.axdbdocument dbxdoc;
24: dbxdoc = (dbx.axdbdocument) acadapp.getInterfaceObject (progID);
25: dbxdoc.open (@ "f: /test.dwg");
26: Foreach (dbx.acadentity Entity In dbxdoc.modelspace)
27: {
28: if (entity.entityName == "acdbblockreference") // determines if the entity is block 10: {
30: dbx.acadblockreference BLKREF;
31: BLKREF = (dbx.acadblockreference) Entity; // will be a block reference entity forced to block reference type
32: Object [] atts = (object []) BLKREF.GETATTRIBUTES (); // Gets the properties in block reference (for object type)
33: for (int i = 0; i 34: { 35: dbx.acadattributereference att; 36: ATT = (dbx.acadattributereference) Atts [i]; // Convert block reference attribute (object type) to block reference attribute type 37: console.writeline ("tag: {0} / TValue: {1} / n", 38: att.tagstring, 39: att.textString; // Display block reference attributes TAG and TEXT value 40:} 41:} 42:} 43: console.readline (); 44:} 45:} 46:} 47:} The first one should pay attention to the third line, using an alias. Because AutoCAD and ObjectDBX namespace have many identical class names, you have to use a full name, but you can't use a short-write form, but ObjectDBX is more troublesome, so use alias DBX to make the input is convenient. The code in front of the program, you can refer to the article I wrote "Secondary Development of AutoCAD" with C # ". Let's see row 21, the program defines a string ProgID, as a parameter of the function GetInterfaceObject of the 24th sentence, the function is used to generate an AXDBDocument object. But note that the type returned by the GetInterfaceObject function is Object, so you have to turn it into the AXDBDocument class with forced conversion. Then, on the 25th line, the AXDBDocument object generated by the 24 line is "Open" one .dwg file (actually not open), you need to pay attention to the path of this file must be correct. This file is the file where the block information we have to get. Since ObjectDBX has no selection set, only the block information (26-28 rows) is obtained by the model space of the traversal file. The description of the remaining statement I have written in the comment of the program. You can find that ObjectDBX works in C # is similar to VBA, but only necessary types of conversions.