AutoCAD-Databases
AutoCAD-Databases
(OP)
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
But I would like to save the attributes when the user saves the draw, not when he insert the block...
thanks in advance





RE: AutoCAD-Databases
RE: AutoCAD-Databases
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.
RE: AutoCAD-Databases
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
RE: AutoCAD-Databases
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
RE: AutoCAD-Databases
RE: AutoCAD-Databases
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.
RE: AutoCAD-Databases
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.
RE: AutoCAD-Databases
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.
RE: AutoCAD-Databases
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...
RE: AutoCAD-Databases
;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)
)
RE: AutoCAD-Databases
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.
RE: AutoCAD-Databases
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.