Jump to content

Importing polygon from a csv file


labmonkey

Recommended Posts

Hello,

 

I have searched through the forum and google, but I couldn't quite find the answer to this question, so I hope somebody here can help me.

 

Is there a simple way to import a polygon from a csv file? I have read about ascpoint.lsp, but as far as I could work out, this makes a polyline, not a polygon. A simplified example is that I want to draw a rectangle with points 0,0 and 10,5 (note there is no z-axis, i.e. it's all just 2D). How can I create this from a csv file? I am flexible as to the format of the csv, but for example it could be

0 0 10 5.

 

Without trying to overcomplicate things, it would also be useful if the colour could be defined in the same csv file (e.g. using a 5th column, or a second row) and that the polygons were automatically filled solid (as you could do by using the BHATCH command).

 

The main reason I ask is that I have some cel files containing polygons and lines, but I cannot import them into autoCAD directly (changing the extension to DGN and inserting it fails) possibly due to a weird/obsolete format, however, I can convert the cel file to a csv file and strip out the nonsense. If it helps I could add a copy of an example cel file to a reply to this thread.

 

Many thanks in advance.

Link to comment
Share on other sites

  • Replies 37
  • Created
  • Last Reply

Top Posters In This Topic

  • labmonkey

    19

  • MSasu

    11

  • Blackfish

    4

  • fuccaro

    2

Unless I’m missing something in your explanation, an AutoCAD rectangle is nothing more than a polyline.

What information is stored in the said CSV? Is about the insertion point and dimensions of rectangle, or coordinates of the corners? You have more than one case described in a singe CSV or many CSV files?

 

The easiest way to solve this will be to open the CSV file in Excel and build a script to automate the drawing operations:

 _RECTANGLE 0,0,0 10,5 _HATCH _P _S _L  _CHPROP _L  _C 5 

Please pay attention that the line should end with an space character.

 

Use SCRIPT command to run it.

Link to comment
Share on other sites

Mircea,

 

Thank you so much for your reply. These forums are extremely useful when you don't know the best approach to solve the problem.

 

Firstly, sorry, I realise my mistake with the polyline/polygon in ascpoint.lsp. I had set my csv file up so that it wouldn't close the polyline. I needed a 5th set of coordinates (with the 5th being the same as the first) so that it 'knows' to close the polyline, i.e. switch it from three lines, to a rectangle.

 

Secondly, I think you're idea of the scrip is great. I will give it a go and reply to say if it works or not. Presumably this approach can be used to make multiple polygons in the same script? (To answer your question, yes there will be many polygons in my CAD file.)

Link to comment
Share on other sites

You may have how many lines/operations you want in the script:

_RECTANGLE 0,0,0 10,5 _HATCH _P _S _L  _CHPROP _L  _C 5 
_RECTANGLE 15,9,0 20,17 _HATCH _P _S _L  _CHPROP _L  _C 2 
_RECTANGLE -15,-9,0 -20,-17 _HATCH _P _S _L  _CHPROP _L  _C 7 
_ZOOM _E
;end of script

Link to comment
Share on other sites

It really varies, it could be very many (more than one hundred).

From your post I think it will not be difficult to generate this. I shall give it a go and post later to tell you how I got on (or to ask further questions:lol:).

I'm in Japan, so I will have a bit of a break now, but will hopefully have time later to finish this.

Thanks once again.

Link to comment
Share on other sites

You're welcome!

I suggested you to use Excel (or other worksheet application for that mater) since there you have built-in features to clone the code from first line to all subsequent ones.

 

There should be night at this time in Japan, right? It may be good to fill the location field in your Profile; didn't noticed yet another user from Japan.

Link to comment
Share on other sites

Will update my profile to make it up to date.

To save me a little time, could you tell me how I could use the PLINE command?

e.g. instead of

_RECTANGLE 0,0,0 10,5 _HATCH _P _S _L  _CHPROP _L  _C 5 

have

_PLINE for the equivalent shape.

Also, with your above code the lines are all the same colour (red, which I guess is the default). How may I change that?

If you're busy, please don't worry, I think I can work it out for myself after dinner.

(It's only 6pm here, but I think I will go home soon to eat and have another crack at this afterwards.)

 

I honestly never knew you could use scripts in autocad. I get the feeling I may have saved a lot of time if I knew before.

Link to comment
Share on other sites

Just follow the succession of prompters for the desired command - you may test it first on command line; please pay attention to call command line version of commands with dialog boxes (i.e. "-HATCH" instead of "HATCH").

To address your question:

_PLINE 0,0,0 10,0 10,5 0,5 _C _CHPROP _L  _C 3  _HATCH _P _S _L  _CHPROP _L  _C 5

Link to comment
Share on other sites

Also look at "read file" the same thing can be done without rewriting the csv to create a script using lisp you can read a line in work out the L & W and insertion pt and create a rectang or a pline, the lisp just keeps going till it hits end of file.

 

How can I create this from a csv file? for example 0 0 10 5. A csv is a comma seperated variable file 0,0,10,5 you have a space delimited file, good news it doesn't matter just need to tell lisp to use space or ,

 

Post a few lines of the csv

Link to comment
Share on other sites

Mircea,

 

I think I've almost got it now. I have one problem, summed up in this example script;

_LINE -96.860,32.583 56.082,32.583  _CHPROP _L  _C 2  _HATCH _P _S _L  _CHPROP _L  _C 1 
_RECTANGLE -69.138,-14.067 -15.304,14.531 _CHPROP _L  _C 2  _HATCH _P _S _L  _CHPROP _L  _C 2 
_COLOR 3 

_MTEXT 10,15 _H 2 15,17 hello  

_COLOR 6 

_MTEXT 10,13 _H 2 15,15 hello  

_PLINE -79.289,-229.289 -79.289,-200 20.711,-200 20,711,-229.289 _C  _CHPROP _L  _C 6  _HATCH _P _S _L  _CHPROP _L  _C 6 
_ZOOM _E 

The last pline does not close. I thought that _C should close the line. As it is, it hangs as it is still looking for the next pline point. Any ideas? Am I missing some code?

 

I think that is all I need to do script wise. I now have to decide the best way to generate the script, i.e. how to get the coordinates out of the existing *.cel file. Originally I was going to write a macro in excel, but there may be an easier approach. For your information, the *.cel file has lines like

SD 600000.000000;
DZ TL-0.079289,-0.229289; TL-0.079289,-0.200000; TL0.020711,-0.200000; TL0.020711,-0.229289;

where the numbers in the second line are coordinates for a pline. I think it will be relatively straightforward to do extract the useful information, but I'm not sure of the most efficient way of doing it right now. I will try in excel and see where that gets me. The number in the first line is related to the colour I'd want for the lines/bhatch, but that's beyond what I want to do now.

Link to comment
Share on other sites

Thanks BIGAL,

 

I will look into that. That may be simpler than using an external program (such as excel). In my previous post you can see some example of the original *.cel file. There are also a lot of lines containing 'nonsense', but they should be easily disgarded. My idea for using excel was that I can strip out the nonsense relatively easily and then insert cells where I need to containing commas, spaces, and text such as " _C _CHPROP _L _C 3 _HATCH _P _S _L _CHPROP _L _C 5" and then re-save it as the script.

 

Also look at "read file" the same thing can be done without rewriting the csv to create a script using lisp you can read a line in work out the L & W and insertion pt and create a rectang or a pline, the lisp just keeps going till it hits end of file.

 

How can I create this from a csv file? for example 0 0 10 5. A csv is a comma seperated variable file 0,0,10,5 you have a space delimited file, good news it doesn't matter just need to tell lisp to use space or ,

 

Post a few lines of the csv

Link to comment
Share on other sites

So, I'm still confused with this script.

_PLINE -79.289,-229.289 -79.289,-200 20.711,-200 20,711,-229.289 _CLOSE 
_LINE -96.860,32.583 56.082,32.583 
_ZOOM E 

(edit: there is a space after "_ZOOM E", but it's not showing above for reasons I guess due to the forum software.)

If I literally copy the above code and paste it into the command line it does exactly what I expect and this is seen in the command line window.

Command: _PLINE

Specify start point: -79.289,-229.289

Current line-width is 0.0000

Specify next point or [Arc/Halfwidth/Length/Undo/Width]: -79.289,-200

Specify next point or [Arc/Close/Halfwidth/Length/Undo/Width]: 20.711,-200

Specify next point or [Arc/Close/Halfwidth/Length/Undo/Width]: 20,711,-229.289

2D point or option keyword required.

Specify next point or [Arc/Close/Halfwidth/Length/Undo/Width]: _CLOSE

Command:

PLINE

Specify start point: _LINE

Invalid point.

Specify start point: -96.860,32.583

Current line-width is 0.0000

Specify next point or [Arc/Halfwidth/Length/Undo/Width]: 56.082,32.583

Specify next point or [Arc/Close/Halfwidth/Length/Undo/Width]:

Command: _ZOOM

Specify corner of window, enter a scale factor (nX or nXP), or

[All/Center/Dynamic/Extents/Previous/Scale/Window/Object] : E

But, if I try to tun it as a script (there is nothing else at all in the script), it fails. It hangs while asking for the next point for the PLINE. This is what is seen in the command line window.

Command: script

Command: _PLINE

Specify start point: -79.289,-229.289

Current line-width is 0.0000

Specify next point or [Arc/Halfwidth/Length/Undo/Width]: -79.289,-200

Specify next point or [Arc/Close/Halfwidth/Length/Undo/Width]: 20.711,-200

Specify next point or [Arc/Close/Halfwidth/Length/Undo/Width]: 20,711,-229.289

2D point or option keyword required.

Specify next point or [Arc/Close/Halfwidth/Length/Undo/Width]: *Cancel*

(The last cancel is after I hit escape to stop it from hanging.)

 

Any ideas? It is really confusing me.

Link to comment
Share on other sites

Please check that you have used a comma (",") instead of a dot (".") which translate last input into a 3D point - the polyline command will accept a 3D point argument only for first point, all subsequent ones must be 2D:

_PLINE -79.289,-229.289 -79.289,-200 20.711,-200 20[b][color=red].[/color][/b]711,-229.289 _CLOSE 

Link to comment
Share on other sites

:D

Thanks!

I cannot believe I was so dumb!

I've been staring at that code for ages, and never noticed I'd accidentally used a comma instead of full stop!

I think I have a follow up question in a couple of minutes.

Link to comment
Share on other sites

You're welcome!

The "trick" is to check careful the messages AutoCAD is issuing to you; on the troublemaker input it was complaining that the format is not expected one for a 2D point.

Link to comment
Share on other sites

You're welcome!

The "trick" is to check careful the messages AutoCAD is issuing to you; on the troublemaker input it was complaining that the format is not expected one for a 2D point.

Thanks. I was so convinced that the error was about closing the pline that I didn't consider I had a problem before that. I spent ages today trying various combinations of _CLOSE, _close, -CLOSE,.... etc.

 

My follow up question (and hopefully last) is about this code, which is the shell of my final script.

_LINE -96.860,32.583 56.082,32.583  _CHPROP _L  _C 2  _HATCH _P _S _L  _CHPROP _L  _C 1 
_RECTANGLE -69.138,-14.067 -15.304,14.531 _CHPROP _L  _C 2  _HATCH _P _S _L  _CHPROP _L  _C 2 
_PLINE -79.289,-229.289 -79.289,-200 20.711,-200 20.711,-229.289 _CLOSE _CHPROP _L  _C 3  _HATCH _P _S _L  _CHPROP _L  _C 3 
_COLOR 1 

_MTEXT 20,5 _H 1 20,10 1.0  

_COLOR 2 

_MTEXT 20,3 _H 2 20,8 2.0  

_COLOR 3 

_MTEXT 20,1 _H 2 20,6 3.0  

_ZOOM _E 

It seems to work completely fine the first time I run it. However, if I run it a second time (without either erasing or zooming or panning the drawing) some of the text in the _MTEXT command gets shifted. I don't think this will be a problem because I will only ever run the script once, but I am curious why it does it. Probably I am using the _MTEXT command incorrectly. I simply want (in this case) three lines of text, each in a different colour). Should I be clearing the memory somewhere?

 

Apart from that, my next problem is how to extract the coordinates from the original *.cel file. I'll probably use some external program which I am more knowledgable about.

Link to comment
Share on other sites

It seems to work completely fine the first time I run it. However, if I run it a second time (without either erasing or zooming or panning the drawing) some of the text in the _MTEXT command gets shifted. I don't think this will be a problem because I will only ever run the script once, but I am curious why it does it. Probably I am using the _MTEXT command incorrectly. I simply want (in this case) three lines of text, each in a different colour). Should I be clearing the memory somewhere?

 

I can run the above script as many times as I want and nothing is shifted; text, line and polylines are exactly as they should be (why not?) in the same place with or without zooming or panning.

Link to comment
Share on other sites

I was so convinced that the error was about closing the pline that I didn't consider I had a problem before that. I spent ages today trying various combinations of _CLOSE, _close, -CLOSE,.... etc.

Always keep in mind the an error message is issued right after the trouble-maker input; is also true that for some cases the processing is resumed and the following inputs are retrieved on prompter that may cause more errors. So, focus to solve the first one, since the following others may be just consequences.

Link to comment
Share on other sites

I'll try it on another computer later to make sure it's not just my machine.

Please try to disable the Osnap before run the script. Seems that, at least in AutoCAD 2010, the MText are affected by this feature.

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...