Jump to content

Area Extraction


harilalmn

Recommended Posts

Hi All,

I have some Polylines drawn for Areas of Rooms.

I also have Room Tags which are Blocks using Multiline Attributes for Room Names.

I have to do a program which loops the process of picking each pline followed by the corresponding Room Tag and finally write the whole data to a csv file.

Data to write is something like;

 

Room1, 50.00, 30.00

 

Room1 is extracted from the Multiline Attribute. 50.00 is the Area and 30.00 is the Perimeter.

 

Here is the code.

But it creates trouble with the Multiline attribute.

 

(defun c:ArToEx()
 (setq n 1)
(while (> n 0)
  	(Progn
	  	(prompt "\n")
 			(command "AREA" "e" (entsel))
			(setq ent-area (getvar"area"))
	  	(princ)
  		(setq Perimeter (getvar"perimeter"))
	  	(sk)
	  	(setq n (+ 1 n))
	)
)
)


(defun sk (/ dxf ent)

 (defun dxf (code ent) (cdr (assoc code (entget ent))))

 (if (and (setq ent (car (entsel "\nSelect an Attributed Block: ")))
          (eq "INSERT" (dxf 0 ent))
          (= 1 (dxf 66 ent)))

   (while (not (eq "SEQEND" (dxf 0 (setq ent (entnext ent)))))
(progn
     	    (princ (strcat "\n---------------------------------------- " (dxf 1 ent)))
   	    (prompt (strcat ", " (rtos ent-area)))
    (prompt (strcat ", " (rtos Perimeter)))
)
   )
 )

 (princ))

 

Right now I made it to prompt the data on command line. I dont know how to export it as csv.

Also the Multiline Attribute brings trouble.

 

Please help...

Link to comment
Share on other sites

Yes the multiline attribute could have some hassles when working directly in DXF. It's not as simple as just checking the 1 DXF code anymore, it becomes similar to MText and could have several 3 codes as well. For this I'd suggest you rather try using the activex object of the attribute and then use its vla-get-TextString function. Also the multiline could contain formatting codes (especially new-lines), so you might want to convert them to some other type of character - you may want to use the vl-string-translate for that one :wink:

 

As for writing to CSV, that's quite "simple": You open a text file using something like:

(setq f (open "c:/path/filename.csv" "w"))

I'm using the forward slashes in the path instead of double back-slashes (but they both work the same). The "w" at the end tells the open function that you want to either overwrite or create a new file. A "r" would open the file in read-only mode, and "a" in append mode.

 

After that the f contains the opened file handle. So you can add that to any of the prin* finctions:

(princ "This line will get written to the file" f)

Now to add newlines it's usual to add a return & newline character at each end-of-line:

(princ "\r\n" f)

So basically what you;ve got in that princ & following promt calls (just change the prompt to princ as well and add the file's handle to the end as argument.

 

After you've written everything you want to the file use (close f) to release the handle so other programs may then open it.

 

I'd also suggest that you wrap any text values in quotes, if there's a comma in the text it could screw with the CSV file as well. What you'd want is something like this:

"Room1",50.0,30.0

 

That way even if the room's description contains a comma, it won't go an add an extra column when opened in Excel.

Link to comment
Share on other sites

Why would you not add the room area and perimeter to the room block and it could be a field so if shape is changed the values are updated, you can hide attributes from view if you want. It make sense to me to do only one step not two. You can use all the tools like dataextraction then easily.

Link to comment
Share on other sites

BIGAL,

Thanks for your reply. I could make a block for the room, if the room is Rectangular or any regular shape. In this particular case the rooms have irregular shapes with curves and angular lines. So making a room block doesn't work.

Link to comment
Share on other sites

irneb,

Thanks a lot... Your reply was much useful.

But how can I save the csv file at the same location as the drawing file?

I tried something to extract the path information of the drawing with;

(setq Path (getvar "DWGPREFIX"))

But the path string has "\" which is not acceptable by lisp.

How could I do it?

Link to comment
Share on other sites

BIGAL's suggestion works on other shapes than just rectangular. All you need to do is place a field into the attribute pointing to the area property of the polyline. I use this way extensively, it's even then possible to copy the room tag together with the polyline and then adjusting the new polyline to different shape/size. Look at the code for DimArea in my Caddons/DimUtils.LSP. It places a field to multiple polylines (added and/or subtracted) into any text/mtext/attribute.

 

Anyhow, that DwgPrefix gives just the folder path in which the drawing is saved. You need to append a filename to it (you can't save directly into the folder itself). So you could use something like this:

(setq filename (strcat (getvar 'DwgPrefix) "MyLispFileName.LSP"))

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...