Jump to content

odd behavior from autoLISP


ManiacalDigits

Recommended Posts

whoops - yes, before sorry!

 

There is a reason I usually go with entmake, don't need to worry about that ( I did a check 450+ LISPs in my main library and I have used "_non" in 8! Not something I use every day)

 

Edited by Steven P
  • Like 1
Link to comment
Share on other sites

@Steven P well, using  "_non"  before the point seems to work, even with o-snaps on.  so good tip.   that was baffling me. 

 

Ok, ready for more.  I'll lookup entmake see what I can figure out.  Thanks again!

 

 

 

  • Like 1
Link to comment
Share on other sites

So Entmake is an alternative method for creating entities. If you type:

(entget (car (entsel "Select an entity")))

 into the command line and select something you'll get something like this:

((-1 . <Entity name: 26308e8d720>) (0 . "LINE") (330 . <Entity name: 2635ff791f0>) (5 . "43F2") (100 . "AcDbE....

 

You probably know this. The list items in brackets for example (0 . "LINE") are dotted pairs, the first number is the reference the second is the value it refers to. Here, 0 and "LINE"

 

With entmake you are creating an entity directly with these codes and bypassing the CAD user interface commands.. so it can be a little quicker (like a second every 100 entities created, not enough to get a drink), but you also get a little more control perhaps.

 

There are lists out there with the bare minimum dotted pairs to create an entity, some are optional and revert to default. For example the code 62 generally refers to colour, if you don't use it, you get whatever it is set at in your system.

 

A basic overview.

 

So jumping back to my first line here, you can interrogate any entity to see what is required in its creation. Some like points are very simple, some like polylines are more complex. A handy thing to refer to when entmake -ing entities.

 

.... you did say ready for more?.....

 

I have a library of entmake LISPs and just call them as I want. So in the LISP above you might create this somewhere to create a LWPolyline:

 

(defun MakeLWPoly ( lst cls lay / MyLWPoly )
  (setq LWPoly (entmakex (append (list
          (cons 0 "LWPOLYLINE")
          (cons 100 "AcDbEntity")
          (cons 100 "AcDbPolyline")
          (cons 8 lay) ;; Not necessary, can be commented out, it is useful
          (cons 90 (length lst))
          (cons 70 cls)
        ) ; end list
        (mapcar (function (lambda (p) (cons 10 p))) lst)
      ) ; end append
    )  ; end entmakex
  ) ; end setq
); end defun

 

and in your LISP call it with

(setq NewLWPolyLine (MakeLWPoly lst cls lay) )

 

Where lst is a list of points for the polyline, cls is whether it is closed (1 for closed, 0 for open, as numbers not a string) and lay here is the layer for the polyline. lay isn't necessary, see the note in the code too.

 

 

So try it and see how it works, maybe see if you can add in a colour which will require a colour index colour (number 0 to 255). Sometimes the order of the list is important so use the first line (entget...) to work out where it insert dotted pairs in the same order

 

Last couple of notes, in the list above I use cons to create the dotted pairs and allows the routine to use variables, you can also use '(0 . "LINE") for example if that never changes. You don't need to specify entiy name - CAD will do that on creation

 

And Entmake or Entmakex, entmakex returns the entity name, entmake doesn't so in my calling line I can use setq and have a reference for the new entity created - with entmake you might need to use (entlast) to get that reference.

 

 

 

Final final note, from this you can go on to entmod and modifying existing entities through the entity definition directly.

 

 

 

 

 

Edited by Steven P
  • Agree 1
Link to comment
Share on other sites

3 hours ago, Steven P said:
(mapcar (function (lambda (p) (cons 10 p))) lst)

whats p?

 

is there a list of these codes like 62 for color, 100 for defining type, etc. etc?   Or is the only way to find out by using entget?  

 

Just so you get why I'm doing this.... I am going to use the output of this to generate  .NC (g-code) for our CNC tables.  to cut material.  I only need to get that code to our CNC controller software.  easy peasy. 

 

Our software that generates that code prefers to figure out how deep to cut from the z-plane thickness of the drawing.   we use 2 -3 different thickness's and it would be nice to set that programmatically rather than mouse clicks.  anyway that is the point of the exercise.  I actually easy access to the properties tab and can do it in 2 mouse clicks and 4 keystrokes.  but its conditional.

Edited by ManiacalDigits
Link to comment
Share on other sites

So the (function (lambda .. line is like a mini function within the LISP, instead of making up a seperate function, and the p in this case is a variable in that 'function', is it runs it picks up what is in lst. Could be seen to be similar to the n in (foreach n lst.... ) loop im this case.

 

 

 

There is a list of the codes on the Auodesk / cad help pages, search dxf codes, many are common but some entities have some differences:

10 is usually a point, a main point perhaps

11 is usually also a point, end of a line or insert point for text justified apart from top right

62 usually the colour

8 usually the layer

0 for the entity type

There are usually 2 off 100s which describe the entity type

 

and most of the rest are secific to the entity type

 

there are also often a -1 and a 330 in the group which you don't need to worry about - CAD will sort them

Edited by Steven P
Link to comment
Share on other sites

wow.... where is the manual????    i long for the good old days when i was learning to program on my C-64.

 

alright, I'm gonna try this... I'm not fast.   and trying to wrap my head around this is rough.   I started learning LISP about 6 weeks ago, because I had to. <sigh>

 

I'll letcha know what happens.

  and thank you very much for your help. 

 

Edited by ManiacalDigits
  • Like 1
Link to comment
Share on other sites

I'm sure it is all written down officially from AutoCAD too but I have learnt what I need as I need to, and copy interesting stuff when I see it.

Link to comment
Share on other sites

@Lee Mac that is perfect thanks!   I often spend more time searching for the documentation than reading it.

 

@Steven P @BIGAL while I was trying to figure out entmake it occurred to me that I already have the entity and all I need to do is (setpropertyvalue (entlast) "Thickness" value).  Which does exactly what I needed. 

 

My main takeaways from this exercise are:

1. Localize critical variables (you never know what else is running). 

2. Clean up your code. all those 1 condition conditions were not helping. 

3. Make sure all your variables are accounted for.   I declared MRAD when I collected the projection and width (which I do for all the options) but changed that and forgot to declare it in option 3. nice catch.  

4. consider your environmental settings when troubleshooting odd behavior!  o-snap is awesome.   it also messes things up. consider alternatives.

 

This program is ready for use now!  Can't thank you folks enough for all your help.  I've learned more than I ever wanted to about LISP!  its going to be useful moving forward.

flatcalc.lsp

Edited by ManiacalDigits
  • Like 1
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...