Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations GregLocock on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

AutoCAD-Databases 2

Status
Not open for further replies.

Pipelinhas

Computer
May 21, 2001
18
Can I save block attributes of an autocad draw to a database (access, oracle, ...) ?
But I would like to save the attributes when the user saves the draw, not when he insert the block...

thanks in advance
 
Replies continue below

Recommended for you

To do this, you would need a VBA application to access Excel and to create a command reactor to autoexecute the program to export the data. There is a VBA sample application included with R14 which exports attributes to Excel. All you would need to do is add the command reactor. For more info on VBA visit they have a good newsletter you can subscribe to as well.
 
Well, Striker, I looked on the said site but could not see something for Acad. Still, maybe I missed. But f it is not there, I could post the lisp routine here:

Different blocks in the drawing have different numbers of attributes and different prompts. So, should each block name go into a separate file named after the block?

Two options - which one should it be

1.
ExtAttsToTxt command, and pick a block - attribute prompts of this block will be exported to a txt file, comma separated, and attribute values of all blocks of the same name will be exported too. That means, One txt file per block name.

2.
ExtAttsToTxt command, and all attribute values of all blocks go to a txt file - all instances of a block and then next block etc.

The txt file can be imported from Access or other Dbase programs.

Lets have other approaches on this please before I sit dwn to write the code.

Thanks.
 
Thanks tigrek.
The thing is that I've already done a code like that. That is, I'm saving all the block's attributes in a drawing to a txt file.
But this way I can't make it totally automatic, because I have to pick up the txt file and convert it to a database.
What I thought I could do was making a vb script that automatically could send the attributes to a database. Is it possible ? I never worked with vba
 
Thanks tigrek.
The thing is that I've already done a code like that. That is, I'm saving all the block's attributes in a drawing to a txt file.
But this way I can't make it totally automatic, because I have to pick up the txt file and convert it to a database.
What I thought I could do was making a vb script that automatically could send the attributes to a database. Is it possible ? I never worked with vba
 
As I said before, there is a VBA Application that will directly export all attributes to an Excel spreadsheet included in the sample folder in either your AutoCAD R14 folder or the ACAD2000 folder. This globally extracts all attributes, but could easily be modified to extract only the ones you specify. The way to make it execute upon saving is to create a command reactor that will execute upon excuting a command. The command reactor can be any of a multiple of possibilities under the AcadDocument command. You could use either AcadDocument_BeginCommand and test for the command issued, or you could use AcadDocument_BeginSave, and just have it execute then. There are more possibilities, also in A2k you have even more. The vbdesign website is a site that offers help with VBA programming and has been an invaluable source for me, I would suggest getting involved with the "Code a Day" newsletter, even though it is only once a week :) It will provide insight into VBA that you never dreamed possible.
 
Well, if you have the possibility to export to txt file,
I think the next step should better be from the other application, to import the txt file.

Otherwise, the combination will be unique to your sysytem and not useful elsewhere, and frankly speaking, this makes it unfeasible to write.

To keep the txt as interface between applications and not automate further for transfer of data from application to application allows building blocks useful to many instead of for only one instance.

Otherwise, of course it could be solved. Sure with VBA it is a simple task.
 
The problem with saving the attributes in a txt file is that if the user changes the drawing, I've to find out that the change has been made and then import the txt file again.
I think that it would be easiest if I could make the attributes to be directly export to te database.
Is it possible ? and how ? :)

thanks again.
 
Sure it is possible - with VBA
but the code depends on the particular database.
There cannot be a general macro for all databases.

Therefore, the suggestion was, the database program should have a macro that on opening, imports the txt file that is in a certain directory -

as opposed to aa VBA macro in AutoCAD that writes into the database.

To write into Excel, for example, is practical, because Excel is used by more people than a specific Database is.

I have written one such VBA program in AutoCAD that exports/imports Attributes from to Excel.

Without having the database you work with, I could not test the VBA to see if it can write to the files of that database.

Otherwise, I would do it with pleasure.
 
Ok, so do you mean that I can do it if I know in advance what database I'm using ?
That's fine because I do have a specific database.
I don't want to be abusive, but can you send me a code example for an attribute extrattion ?

For example if all the blocks that I alow the users to input in the drawing have 2 attributes (code, definition) and I would like to export those attributes to an access database (att.mdb) with the fields (code, definition), how can I do that ??

thanks in advance tigrek...
 
;Block Attributes to Access (via txt linked to access)

;The following is a variant of blk_lst.lsp of Bonus/Cadtools of Acad14
;it may be missing in A2000 though.
;This works for one block picked at a time.
;Try it -if the linking of txt file to Access is acceptable,
;we shall preceed this routine with an ssget function
;to make it do for a bunch of blocks at one pick.

;please modify this line, down below, before use.
;Put here the txt file name linked in your Access Databank.
;(setq f1 (open "C:/Acad/MyFile.txt" "a"))

;Aceess allows directly linking of txt files - over Menu>Files>
;So, a comma separated txt file is practically an access table.
;and the table updates automatically each time Access is opened.
;the txt is linked only once.

;See if comma separation is enough. If not, we can add the quote marks in the code below.

;Frankly, it is too much trouble to write directly into Access mdb file, given the convenience
;of linking txt to Access.


;for each block
(defun c:attribex (/ nam b en1 en constcnt varcnt)
(setq myLine "")
(setq en (car (entsel)))
(if (not en) (progn (prompt "\nNothing selected.")(quit)))
(if (/= "INSERT" (dxf 0 (entget en))) (progn (prompt "\nNot a block.")(quit)))
(setq bnam (dxf 2 (entget en))
constcnt 0 varcnt 0 en1 en)

; list constant attributes:

(if (setq b (tblsearch "block" bnam))
(if (setq en (dxf -2 b))
(progn
(pratt en "ATTDEF" 'constcnt)
(while (setq en (entnext en))
(pratt en "ATTDEF" 'constcnt)))
(prompt (strcat "\nNo entities in block " bnam)))
(prompt (strcat "\nBlock " bnam " not found.")))

; list variable attributes:

(while (/= "SEQEND" (dxf 0 (entget (setq en1 (entnext en1)))))
(pratt en1 "ATTRIB" 'varcnt))

(prompt (strcat "\nBlock " bnam " has " (itoa constcnt) " constant attributes."))
(prompt (strcat "\nBlock " bnam " has " (itoa varcnt) " variable attributes."))
(princ "\n")(princ myLine)
(AppendToFile myLine)
(princ)
)
;---------
(defun dxf (n ed) (cdr (assoc n ed)))
;--------
;==================================================================
; pratt - print attribute ins point, tag, and value & incr counter:

(defun pratt (en type pcnt)
(setq ed (entget en))
(if (= type (dxf 0 ed))
(if (or (/= "ATTDEF" (dxf 0 ed)) ; variable attribute: ATTRIB
(/= 0 (logand 2 (dxf 70 ed)))) ; const: ATTDEF & const flag
(progn
(set pcnt (1+ (eval pcnt)))
;(print (dxf 10 ed))
;(prin1 (dxf 2 ed))
;(princ ": ")
(setq myLine (strcat myLine (dxf 1 ed) ","))

;(prin1 (dxf 1 ed))
)))
)
;-----
(defun appendToFile(MyLine)
(setq f1 (open "C:/Acad/MyFile.txt" "a"))
;(princ "\n")(princ myLine f1)(princ "\n")
(write-line myline f1)(print)
(close f1)

)
 
Ok, I see what you mean.
The code is all in AutoLisp rigth ?
That's the same that I already have in use. It does extract all the attributes to a txt file. But the problem is that I would like to export the attributes directly to the database. (In real an Oracle database which is in a remote server - Intranet IP server). Am I being illustrative ?
But I don't know if it is possible.
 
Well, what I am saying is,
why does it matter how the data gets to Database,
since the txt file
that is a container for the attributes,
will be linked to the database only once.

To rearrange the values in different tables is
better odne within the database.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor