Extract Block Geometery & Attribute

Extract Block Geometery & Attribute

Jonathan3891
Advisor Advisor
2,108 Views
14 Replies
Message 1 of 15

Extract Block Geometery & Attribute

Jonathan3891
Advisor
Advisor

I thought I had this working until I realized that a data extraction template is tied to a single drawing. Data extraction gives me exactly what I want but I want to automate this completely without any interaction from the user.

 

I want to be able to extract pile data (a block) to a csv file containing:

Position X

Position Y

Position Z

Dynamic Block Information

Attribute

 

Is what I'm asking possible without using Data Extraction?

 

I found one on the forums but it wouldn't do anything https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/data-extraction-amp-attributes-on-dy...

 


Jonathan Norton
Blog | Linkedin
0 Likes
Accepted solutions (4)
2,109 Views
14 Replies
Replies (14)
Message 2 of 15

dlanorh
Advisor
Advisor

@Jonathan3891 wrote:

I thought I had this working until I realized that a data extraction template is tied to a single drawing. Data extraction gives me exactly what I want but I want to automate this completely without any interaction from the user.

 

I want to be able to extract pile data (a block) to a csv file containing:

Position X

Position Y

Position Z

Dynamic Block Information

Attribute

 

Is what I'm asking possible without using Data Extraction?

 

I found one on the forums but it wouldn't do anything https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/data-extraction-amp-attributes-on-dy...

 


The lisp in the marked solution of the indicated thread (DBtxt.LSP) has hard wired block names and attribute tag names. These are at the top of the lisp file.

 

	(setq data   nil
	      bnames "SAMPLEB,SAMPLEC,`*U*"
	      Tags '("TAG1" "TAG2")
	      ss     (ssget "_X"
			    (list '(0 . "INSERT") '(66 . 1) (cons 2 bnames))
		     )
	)

Open the file in a text editor and change the items marked in red above to reflect your block name and tag name. Save the file to a slightly different name,  then try loading and running the altered lisp.

I am not one of the robots you're looking for

0 Likes
Message 3 of 15

Jonathan3891
Advisor
Advisor
I've changed the block and tag names in that lisp. I just couldn't get it to do anything.

Jonathan Norton
Blog | Linkedin
0 Likes
Message 4 of 15

dlanorh
Advisor
Advisor

Your block doesn't have a visibility state and thus failed one of the and conditions.


I have modified the lisp (attached) and it should now work with your block, but it won't display the stretch distance.

 

It must be run on a drawing that has been saved at least once, and will write a text file (csv format) containing the attribute value followed by the x,y and z coords.

I am not one of the robots you're looking for

Message 5 of 15

dlanorh
Advisor
Advisor

I realise that this is not in the format you require, but it is to show that data extraction is possible without using autocad's data extraction tool and a template.

 

If you rename the *.txt file to *.csv it should load into excel.

I am not one of the robots you're looking for

0 Likes
Message 6 of 15

dlanorh
Advisor
Advisor
Accepted solution

Try the attached. This collects all the information and sorts by pile number (assuming it is only numeric) and writes the csv to the same directory as the drawing (alterable by changing the path or prompting for a path)

 

I am not one of the robots you're looking for

Message 7 of 15

Jonathan3891
Advisor
Advisor

Thank you for your help.

 

How do I change the units from inches to feet?


Jonathan Norton
Blog | Linkedin
0 Likes
Message 8 of 15

dlanorh
Advisor
Advisor
Accepted solution

If you are refering to the "pile length" dynamic property

 

change the red item below

 

(write-line (strcat (rtos (car i_pt) 2 3) "," (rtos (cadr i_pt) 2 3) "," (rtos (caddr i_pt) 2 3) "," (rtos p_len 2 3) "," val) fp)

to

 

(write-line (strcat (rtos (car i_pt) 2 3) "," (rtos (cadr i_pt) 2 3) "," (rtos (caddr i_pt) 2 3) "," (rtos (\  p_len 12) 2 3) "," val) fp)

This will preserve the drawing units but display the output in the csv file in decimal feet.

I am not one of the robots you're looking for

Message 9 of 15

Jonathan3891
Advisor
Advisor

Thank you @dlanorh for all your help.

 

Can I make one more request?

 

How do I change the information and order to the following:

Pile No, Northing, Easting, Insert Elevation, Pile Length


Jonathan Norton
Blog | Linkedin
0 Likes
Message 10 of 15

Jonathan3891
Advisor
Advisor

Scratch that. I got all that figured out.

 

I do need to control the location of the csv file though. Is that something that can be done easily?


Jonathan Norton
Blog | Linkedin
0 Likes
Message 11 of 15

dlanorh
Advisor
Advisor
Is the csv file location something that can be hard coded or is it something that will vary? Both are relatively easy

I am not one of the robots you're looking for

0 Likes
Message 12 of 15

Jonathan3891
Advisor
Advisor

It will be hard coded.


Jonathan Norton
Blog | Linkedin
0 Likes
Message 13 of 15

dlanorh
Advisor
Advisor
Accepted solution

Find this line in the setq at the top of the lisp :

 

path (getvar 'dwgprefix)

Change it to

 

path "(Drive) :/(Path to Save Location)" ; use "/" as the directory divider i.e. "C:/Users/....../"

Hopefully that should work.

I am not one of the robots you're looking for

0 Likes
Message 14 of 15

Jonathan3891
Advisor
Advisor

Thank you again @dlanorh !

 

Would it be easy to have it check if in model space and if the block exist with an alert? And an alert to upon completions?

 

I've tried but haven't gotten it to work.

 

((alert "Command unavailable in Modelspace"))

((alert "Pile_Block not found!"))

((alert "Pile data exported sucessfuly"))


 

 

 


Jonathan Norton
Blog | Linkedin
0 Likes
Message 15 of 15

dlanorh
Advisor
Advisor
Accepted solution

I have attached an updated lisp that will need editing.

 

The lisp can run from modelspace or paperspace, so there is no need to check. I have included them but commented out so you can see how i have structured them within the cond statement.

 

Below is the top of the attached lisp.

 

I have included a local error function (just in case). The main purpose of this is to close the open file if an error should occur whilst it is open. This error routine replaces AutoCAD's error routine only for the duration of the lisp.

 

The red items are the needed parts for the model/paper space checks. The sections in the "cond" statement have been commented out. If you want to remove them, only remove items coloured red.

 

The orange item is a check to ensure the drawing has been saved. This is to avoid a disconnected drawing csv file pairing. The program will still work if the line is removed.

 

The indigo line is the check for the block.

 

Please note that the path variable will need setting to the path where you require the csv file to be saved.

 

(defun c:EPD ( / *error* c_doc b_nme tag ss path f_name fp val i_pt p_len b_lst)

  (defun *error* ( msg )
    (if fp (close fp))
    (if (and c_doc (= 8 (logand 8 (getvar 'UNDOCTL)))) (vla-endundomark c_doc))
    (if (not (wcmatch (strcase msg) "*BREAK*,*CANCEL*,*EXIT*")) (princ (strcat "\nOops an Error : " msg " occurred.")))
    (princ)
  );end_*error*_defun
	
  (setq c_doc (vla-get-activedocument (vlax-get-acad-object))
        c_spc (vla-get-activespace c_doc);Remove this line if you don't want a model/paper space check
        b_nme "PILE_BLOCK"
  );end_setq
  
  (cond ;( (= acpaperspace c_spc) (alert "Command Unavailable in Paperspace"))
        ;( (= acmodelspace c_spc) (alert "Command Unavailable in Modelspace"))
        ( (= (getvar 'dwgtitled) 0) (alert "Drawing not Saved"))
        ( (not (tblobjname "block" b_nme)) (alert (strcat "Block " b_nme " NOT available in drawing")))
        ( (and  (setq b_lst nil
                      path (getvar 'dwgprefix);CHANGE THIS TO YOUR REQUIRED PATH TO CSV FILE!!! 

I am not one of the robots you're looking for