Take the local library as an example to introduce AutoCAD database connections

zhaozj2021-02-12  212

Take the local library as an example to introduce AutoCAD database connections

Li Ling

(Guangxi first test hospital)

【Abstract】 Take the local library as an example, introduce the AutoCAD2000's database connection function dbconnect. Through the establishment and maintenance process of the local library, the consistency of the database connection, graphics and data maintenance is introduced.

【Key words】 AutoCAD database connection maintenance consistency

1 Introduction

Since AutoCAD R12 introduces AutoCAD SQL Extension (ASE), AutoCAD users have the ability to link AutoCAD objects to external databases. AutoCAD 2000 combines all its database connection functions to form a single easy-to-understand user interface, called DBConnect, DBConnect completely replaces the old ASE interface in the early version of AutoCAD. In AutoCAD 2000, the database connection features Microsoft's OLE DB technology, through a Component Object Model (COM) programming interface --activeX data object (ADO), you can fully control OLE DB. ADO can be used in any computer language that supports COM, also includes an AutoCAD programming environment, such as VBA and Visual Lisp. This paper uses the local library as an example, with Visual Lisp as programming environment, introduces the agocad 2000 database connection and the consistency of graphics and database maintenance.

2 Connections for graphics and databases

In AutoCAD, you must establish a connection to the database before using the database.

2.1 Creating a database connection

2.1.1 Configuring Data Sources

Set D: / Land / Directory with Access Parcel.mdb file, where table Table1, the structure of the table is as follows:

TABLE1:

Id Street No. Luzhang District Land Use Category Rights

Now connect to PARCELS.MDB in a graphic named parcels, and the connection method can be manually connected or automatically connected to the program. The manual connection method is as follows:

1) Open PARCELS.DWG

2) Select DBConnect from the Tools menu. DBCONNECT manager appears.

3) Right-click on Data Sources in DBConnect Manager and select Configure A Data Source from the Dynasty menu to display the Configure A Data Source dialog.

4) Type "PARCELS" as the data source, click OK. The Data Link Properties dialog box appears.

5) Select Microsoft Jet 3.51 OLE DB Provider in the OLE DB Provider list box, press Next >>.

6) Type the full path to the PARCEL.MDB file.

7) Click on the Test Connection check connection to establish.

8) Click OK.

Once the data source configuration is complete, a UDL file is generated in the Data Links directory, and its name appears under the Data Sources node in the DBConnect Manager window.

The process of automatically created a connection is actually the process of creating the UDL file as described above, which is not detailed here.

2.1.2 Get the data source connected to AutoCAD with ADO

Use the loadado function to get the data source connected to the ACAD setting, and connect the database to the following function.

(Defun Loadado (/ gado-dllpath)

(VL-load-COM); loading ActiveX

(setq acadobj (VLAX-GET-ACAD-OBJECT)

Acaddoc (VLA-Get-ActiveDocument Acadobj)

Gado-dllpath "c: // program files // common files // system // ado // msado15.dll") (if (null ado-adomentynamic)

(VLAX-Import-Type-Library

: TLB-FileName Gado-DLLPath

: Methods-Prefix "ADO-"

: Properties-Prefix "ADO-"

: Constants-Prefix "ADO-"

) Loading ADO LIBRARY

)

(setq adoconnect (VLAX-CREATE-Object "AdoDb.Connection"); create a full variable Adoconnect

(Setq Wspath (VLAX-GET-PROPERTY

(VLAX-GET-PROPERTY

(VLAX-GET-PROPERTY

(VLAX-GET-ACAD-OBJECT)

"Preferences"

)

Files "

)

"WorkspacePath"

)

Name (getvar "dwgname")

Len (Strlen Name)

Name (Substr Name 1 (- Len 3))

Name (STRCAT NAME "UDL")

ADSTRING (strcat "file name =" wspath "//" name "; user ID =; password =;")

); Take the data source UDL file path

(VLAX-PUT-PROPERTY Adoconnect "Connectionstring" ADSTRING)

AdoConnect connects Name.mdb to the name.udl file

(VLAX-INVOKE-METHOD Adoconnect "Open" "" "" "" "" "

End loadingado

2.2 Creating a Link Template

2.2.1 Creating a Link Template

Before the link object to the database, the link template must be established first, and the process of establishing the link template is as follows:

1) Open the PARCELS graphic of the connected database.

2) Open the DBConnect Manager and connect to the PARCELS data source.

3) Right-click the Table1 table and select New Link Template. Displays the New Link Template dialog.

4) Change the default name Table1_Datalink1 of the link to PARCELS (same as the graphic name). Click Continue to enter the Link Template dialog, click the box next to the ID to specify the ID as the key column, then click OK. Now you will see the PARCELS node below graphical parcels.

2.2.2 Get Link Templates with CAO

(Defun LoadCao (/ LinkTemplates Name LinkName MdbName Len Index)

(if (null caom-getLinkTemplates); check if Cao Library is loaded

(ProgN)

(VLAX-Import-Type-Library

: TLB-FileName "cao15.dll"

: Methods-Prefix "CAOM-"

: Properties-Prefix "CAOP-"

: Constants-Prefix "Caok-"

) Loading cao liabrary)))

(Setq Caoconnect (VLAX-CREATE-Object "cao.dbconnect"))))

(SetQ LinkTemplates (VL-catch-all-apply 'CAOM-GETLINKTEMPLATES

(List caoconnect acaddoc))

)

(setVar "dwgname")

Len (Strlen Name)

Name (Substr Name 1 (- Len 4))

INDEX 0

Count (VLAX-GET-PROPERTY LINKTEMPLATES "Count")

MDBNAME ""

LinkTemplate NIL

)

(While (

(SetQ LinkTemplate (VLAX-INVOKE-METHOD LINKTEMPLATES "ITEM" INDEX)

LinkName (VLAX-GET-PROPERTY LINKTEMPLATE "NAME")

MDBNAME (VLAX-GET-PropertyLinkTemplate "Datasource")

INDEX (1 index)

)

(if (= name mdbname)

(SetQ Index Count)

)

); Search PARCELS link template in a graphical link template library

(if (/ = name mdbname)

(SetQ LinkTemplate nil)

)

LinkTemplate; Return the function to Table1 Link Template

)

2.3 Automatic links with the database

Draw a weight line in the PARCELS graph, and link it to the database (the graphic handle corresponding to the boundary is used as the ID number of table Table1 in the database, because the graphics handle in AutoCAD is unique):

(Defun draw (/)

(SetQ P (getPoint "/ N rights line first point:"))

(While P

(SetQ PLIST (Append PList (List P))))

(SetQ P (getPoint "/ n Next:"))

)

(if plist

(ProgN)

(SetQ ENAME (LANDPLINE PLIST)); Function LandPline draws the right line according to the node list and returns the corresponding entity name

(APPEND_DB ENAME); add data record corresponding to the weight line in the database

(Makelink ename); link the weight line to the corresponding data record in the database

))

); END Defun Draw

Among them, the append_db and makelink functions are defined as follows:

(Defun append_db (ename / Object ID Area M_plist RS Fields Fields Count Index

Thisfield value)

(Setq Object (VLAX-ENAME-> VLA-Object ENAME))

ID (VLA-Get-Handle Object); handle of the entity

Area (VLA-Get-area Object);

)

(SetQ M_PLIST (List ID "" "" "" "" (RTOS Area 2 6))); predefined data records the value of each field

(COND)

Create The Ado Recordset Object

((NULL (VLAX-CREATE-Object "AdoDb.Recordset"))))))))))) (Princ "/ NUNABLE TO CREATE ADODB.Recordset Object.")

)

((NULL (OK 'Ado-Open (List RS "Table1" Adoconnect Ado-AdoPENDYNAMIC

Ado-AdockOptimistic Ado-AdoptionunSpecified)))))))))))))))))))))))))))))))

)

(T

(ADO-ADDNEW RS); append a blank record in Table1

(SETQ Fields (ADO-GET-FIELDS RS)

Fieldscount (VLAX-GET-PROPERTY FIELDS "count")

INDEX 0

)

(While (

(Setq thisfield (VLAX-GET-PROPERTY FIELDS "Item" INDEX)

Value (Nth Index M_PLIST)

INDEX (1 index)

)

(ADO-PUT-VALUE THISFIELD VALUE); gives the predefined value to each field

); Record blank record each field assignment

(ADO-UPDATE RS); Submit changes to the database

)

); END COND

(ReleaseRS)

); END DEFUN APPEND_DB

(Defun Releasers (RS)

(IF RS

(ProgN)

(if (= (ADO-GET-State RS) Ado-AdStateOpen)

(ADO-CLOSE RS)

)

(VLAX-RELEASE-OBJECT RS)

(SetQ RS NIL)

))

)

The above Append_db joins the ID and the area area in the database record, enabling the zone graphics and database records, and other properties of the locale can be manually entered in the database.

(Defun Makelink (Ename / Vlaobj Objectid Vlax-Obj CurrkeyValues ​​OneKeyValue LinkResult)

(SetQ LinkTemplate (LOADCAO))

(Setq Vlaobj (VLAX-ENAME-> VLA-Object ENAME)

ObjectID (VLA-Get-Handle Vlaobj)

VLAX-OBJ (VLAX-GET-PROPERTY VLAOBJ "ObjectID")

CurrKeyValues ​​(VLAX-CREATE-Object "cao.KeyValues")

OneKeyValue (VLAX-CREATE-Object "cao.KeyValue")

)

(VLAX-PUT-PROPERTY OneKeyValue "FieldName" "ID")

(VLAX-PUT-PROPERTY OneKeyValue "Value" ObjectID)

(VLAX-INVOKE-METHOD CURRKEYVALUES "ADD" OneKeyValue)

(SetQ LinkResult (VL-catch-all-apply 'Caom-Createlink (List linkTemplate Lax-Obj CurrkeyValues)))

(if (VL-catch-all-error-p lineresult)

(Princ "/ n Trying to execute createlink () caused an activeX error:"))

); End Defun Makelink

3 Consistency of graphics and data

Changes in the ground include changes in the nodes of the power boundaries (deletion, increase points, shift points) and segmentation, mergers. The change in nodes only causes changes in graphics and data records, changing links between graphics and data constant; segmentation, merge, not only cause changes in graphics, records, and link relationships between graphics and data change.

3.1 Right boundary line node change

Take the point of movement as an example:

(Defun C: MoveDot (/ ename ss point newpoint oldpoint xylist vlaxobj objid area)

(Setq Point / n Please select the window: [or Enter = exit] "))

(if point

(ProgN)

(setq newpoint (getPoint "/ n moves to point:"))

(SETQ SS (SSGET POINT))

(SETQ ENAME (SSName SS 0))

(setq xylist (getplxy ename); function getPlxy line node list

(SetQ oldpoint (PLIST-DOTAT XYLIST POINT); Function PLIST-DOT Returns Node closest to Point in Node

(Update-ename ename oldpoint newpoint); function update-ename replaces the OldPoint in the boundary object ename into newpoint

(Setq Vlaxobj (VLAX-ENAME-> VLA-Object Ename)

Objid (VLA-Get-Handle Vlaxobj)

Area (VLA-Get-Area Vlaxobj) 2 6)

)

(SetQ SQLSTR (strcat "Update" Table "Set Zong District = '" Area "' Where ID = '" Objid "'")); Define SQL statements

(IF (NULL (OK 'Ado-Execute (List DB Sqlstr Nil Ado-AdoptionunSpecified)); Execute SQL statement, update the area area in the data record

(ProgN)

(Princ "/ NUNABLE TO UPDATE RECORD.")

(exit)

))

)

3.2 Segmentation and mergers

Taking the segmentation as an example, as shown in Figure 1: The weight boundary line is composed of points 1 to 7, and the dividing line is composed of points t1 to point t3, and the dividing line is divided into Z1 and Z2.

(Defun C: split (/ ename1 ename2 VLAXOBJ ID SQLSTR) (SetQ ENAME (CAR (Entsel "/ N selected weight boundaries:"))

(SetQ INAME (CAR (Entsel "/ N selection split line:")))

(SetQ INPLIST (GetPlxy IName)); Function GetPlxy Returns List of Segmentation Nodes

(setq xylist (getplxy ename)); function getPlxy return rights genus list list

(SETQ PLIST); SplitPlist function returns two composite lines after segmentation: ((point 1 point 2 I1 T2 I2 points) (i1 point 3:4:56 6 I2 T2)) (SetQ Xylist1 (CADR PLIST) XYLIST2 (CADR PLIST))

(SetQ ENAME1 (Landpline Xylist1) ENAME2 (LANDPLINE XYLIST2)))

The LANDPLINE function consulates the right line according to the node list and returns the entity name corresponding to the boundaries.

(Setq Vlaxobj (VLAX-ENAME-> VLA-Object Ename)

ID (VLA-Get-Handle Vlaxobj)

)

(setq sqlstr (strcat "delete from" table "where id = '" ID ""))

(IF (NULL (OK 'Ado-Execute (List Adoconnect Sqlstr NIL

Ado-adoptionunSpecified)))))))))))

(ProgN)

(Princ "/ NUNABLE to DELETE RECORD.")

(exit)

)

)

, Execute the SQL statement, delete the data record corresponding to the original weight boundary, and the corresponding link relationship is also deleted.

(Command "ERASE" ename "); delete the original belt line

(append_db eName1); establish a data record corresponding to Zong Z1 in the database

(Make_Link Ename1); connect the Rong Z1's Right Direction Link to the corresponding data record

(APPEND_DB ENAME 2); establish a data record corresponding to Zong Z2 in the database

(Make_Link Ename1); connects the right bound line link of Zongzi Z2 to the corresponding data record

); END DEFUN SPLIT

4 Conclusion

This paper uses the establishment and maintenance process of the local library as an example, which is more detailed to introduce the DBCONNECT function of AutoCAD2000. For the contemplation, the function of the DBConnect has no close contact with DBConnect is slightly not shown. It is hoped that this article can take a reference role to the reader.

references:

[1] (US) Scott McFarlane; Luo Han and other translations. OutoCAD database connection. Machinery Industry Press, 2001.5

[2] Guo Jianbe, Chen Xi, Wang Ning compiled. Develop AutoCAD 2000 applications with Visual Lisp. People's Posts and Telecommunications Press, 2000.1

[3] Tang Jun .autocad2000 Advanced Application and Visual Lisp Development Collection. People's Posts and Telecommunications Press, 2001.1

转载请注明原文地址:https://www.9cbs.com/read-7210.html

New Post(0)