Jump to content

Copy Items From A List


Allan B.

Recommended Posts

I need some help Please. I am trying to copy several items from a list, each item in the list has an entity name and a pick point.I have tried many different lines of syntax but none seem to work.

Example

1.   (foreach pt list (command "copy" pt " "" basept  despt))

2.  (command "copy" (foreach pt list (command pt) "" bapt despt))

 I use autocad r14 I do not think that lisp has changed much since then.  

  I have searched the web but can not find anything that reassembles what I am trying to do. 

The code that I wrote seems to only copy the first item in the list then it quits. 

 

  Thank you Allan

Link to comment
Share on other sites

Hello Allan B. and welcome to CadTutor.   :)

Sorry I am not a member of The Lisperatti, and so am unable to help you with this.

Your first post is very well defined, unambiguous, and I am quite sure that there is no shortage of forum members better suited to the task,

who will get you back on the tracks.

Should you be unfamiliar with, although I doubt it,  LeeMac  just might have something on his excellent site to

help you help yourself.   When in doubt, check LEEMAC out!  :notworthy:

Thanks Lee!   :beer:

 

  • Like 1
Link to comment
Share on other sites

I think "each item in the list has an entity name and a pick point" means something like this:

the list =

item1 = {entity1 pickpoint1}

item2 = {entity2 pickpoint2}

item3 = {entity3 pickpoint3}

...

and so on. (It can be different, check the data yourself)

therefore: (foreach item list ...) will get you iterate through the items, each item = (entity pickpoint)

from that item, to get the entity: (setq entity (car item))

to get the pickpoint (setq pickpoint (cadr item))

Now you want to run copy command, run it over that entity, not that item.

Your (1) structure is a better one, but should be like:

(foreach item list
   (setq entity (car item)     ;change these accordingly to your item data structure
         pickpoint (cadr item) ;maybe this one is the base point to copy? not a fixed basept from somewhere else?
   )
   (command "copy" entity "" basept  despt)
)

 

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

Thank you for the help, but it still quits after the first copy is made. I verified that the entity name was always first in the lists. Some times the list of objects can be 2 other times 3 or 4. I was using the basepoint (bapt) of the first object as the point to ensure that all the objects are picked the same. This way when the copied item is placed they are all relative to the original. In my business I built stair cases, and the programs I have written are over 1500 lines long . So I have used the foreach function many times before with drawing lines no problem. I hope that you can give me another suggestion.    Allan   

    I live in British Columbia  Canada

Link to comment
Share on other sites

  • SLW210 changed the title to Copy Items From A List

Does it quit and show some error? Can you turn CMDECHO on for error message to show up in the command line.

If I were you I will debug it, put a break point there and see what the item is, what the entity is.

Try a breakpoint and put your list to watch window, and show it for everyone can see and help

Link to comment
Share on other sites

2 hours ago, SLW210 said:

I changed the title of your thread to "Copy Items From A List", please use more relevant titles in the future.

What about to use A , as not capital letter , because A could mean the LIST name . 

"Copy Items From a List"

 

Link to comment
Share on other sites

9 hours ago, Allan B. said:

Thank you for the help, but it still quits after the first copy is made.

 

You need to make sure your list is being build properly.

(if (setq SS (ssget '((0 . "*POLYLINE"))))
  (foreach poly (vl-remove-if 'listp (mapcar 'cadr (ssnamex SS)))
    (setq ent (entget poly))
    (setq lst (cons (list (cdr (assoc -1 ent)(cdr (assoc 10 ent)))) lst)) ;build list with point and entity name
  )
)

 

Returns something like this

 

((<Entity name: 5434bb30> (-345.741446253289 85.3279181686597)) (<Entity name: 5434c9b0> (-345.741446253289 78.7997178980733)) (<Entity name: 5434ca30> (-345.741446253289 71.9186419371849)) (<Entity name: 5434c230> (-345.741446253289 65.5668795117495)) (<Entity name: 5434c0f0> (-345.741446253289 58.8622413960121)))

 

The point itself is a list with the entity name

((A (B C)) (A (B C)) (A (B C)) (A (B C)))

 

a = entity name

b = x value of point

c = y value of point

 

This would work with @Linh code

(foreach item list
   (setq entity (car item)     ;change these accordingly to your item data structure
         pickpoint (cadr item) ;maybe this one is the base point to copy? not a fixed basept from somewhere else?
   )

 

(car item) = A

(cadr item) = (B C)

Edited by mhupp
Link to comment
Share on other sites

4 hours ago, Linh said:

Does it quit and show some error? Can you turn CMDECHO on for error message to show up in the command line.

If I were you I will debug it, put a break point there and see what the item is, what the entity is.

Try a breakpoint and put your list to watch window, and show it for everyone can see and help

I think that the problem is that the foreach function does not have a (command pt ) in the copy command syntax to tell foreach to move on to the next pt in the list.

I have tried different scenarios but with no success yet.    Allan

 

Link to comment
Share on other sites

(FOREACH PT (LAST OPTLIST) ; GETS THE LAST 3 ELEMENTS OF THE LIST
  (COMMAND "COPY" PT "" BAPT CORNPT)
   PT ; COPIES THE 1ST OBJECT OK AND RETURNS OBJECT NAME AND COORDS.
 )   ; WHILE NOT PROCEED TO NEXT PT

 

Link to comment
Share on other sites

(LAST OPTLIST) is the last item in the list, not "THE LAST 3 ELEMENTS OF THE LIST"

Another thing is, you don't show us what the Optlist is, so it is hard to guess if there is any other problem.

Link to comment
Share on other sites

THE OPTLIST IS A LONG WITH 13 ELEMENTS IN LIST, EACH ELEMENT CAN CONTAIN ANY WHERE FROM 2 TO 5 ITEMS IN IT , EACH OF THESE ITEMS HAVE A NAME AND A COORDS.

i DON'T THINK THIS MATTERS RIGHT NOW . i AM CONFIDENT THAT THE CODE WE HAVE BOTH WRITTEN WAS CORRECT AS TO GETTING TO THE LIST, THE PROBLEM IS QUITS AFTER THE FIRST COPY, IT RETURNS THE ENTITY NAME  AND THATS IT . IT JUST CAN NOT CARRY ON      ALLAN

Link to comment
Share on other sites

(FOREACH PT (LAST OPTLIST) ; GETS THE LAST 3 ELEMENTS OF THE LIST
  (prompt "\nhello world!")
  (princ pt)
 )

May be you can try above code to see the debug info of each PT item in the last element of the Optlist

Link to comment
Share on other sites

Thank you for helping again.

 I tried your code and

     It returned 2733768 followed by hello world than an entity name 2733738 and coords than another entity name 2733738 with coords.

So I asked (last optlist)  and it returned 2733768 & coords ,2733738 & coords. I believe its ok,  The Foreach just wll not grab the next pt. May be you can try a small test with 2 entities yourself , the problem is not the lists it is differently the syntax . I do not know what to do next. Maybe experiment with mapcar , what do you think    Allan

Link to comment
Share on other sites

I see there is nothing to do here with mapcar or anything else.

Why don't you just copy and paste the orignal result from AutoCAD, like this:

----

Command: (princ (car(entsel)))
Select object: <Entity name: 1b6e1669090><Entity name: 1b6e1669090>
----

Rather than typing it return "2733738 and coords" etc. Command copy can't not do anything with "2733738 and coords ...", i think.

Link to comment
Share on other sites

1. (command pt) is not work. because (command ~~) is not directly write something on the command line.

2. you have list of entity name. not selection set.

 

then you try to base point picking for copy, that's not good way i think. It can be the wrong picking if they overlap.

 

You can also filter that entity name with (ssget "X" with assoc -1..

or

 

 

So I recommend converting the entity name to an object, using 

(setq originalobj (vlax-ename->vla-object entname))

 

then use vla-copy (copy in place)

(setq newobj (vla-copy originalobj))

 

then move with vla-move

(vla-move newobj basepoint destinationpoint)

 

but, vla-move need coordinate form by vla-getpoint. not just coordinates string list.

so, you convert this by

(vlax-3D-point coordinatelist)

or 

(vlax-3D-point x y [z])  ;Z value can be omitted

 

look this link

https://www.afralisp.net/archive/methods/list/copy_method.htm

 

 

Edited by exceed
Link to comment
Share on other sites

(COMMAND "COPY" PT "" BAPT CORNPT)

Using command "copy" is good enough.

Issue here is you don't show us what PT really is. Therefore, I asked to run (princ PT). With "really", I mean, don't try to translate (princ PT) to words like you did. Coders prefer code.

+ If it is an entity name, then the copy command will work, unless, I think it is a multi copy option that ask make the command not end after the above code. Then, you got to put another "" at the end to end the multi copy action.

+ If PT is a list, or a coordinate, that (Command "Copy" PT ...) will fail.

Edited by Linh
Link to comment
Share on other sites

Hi Alan,

Can you post the code so we can see what you are doing? There could be a few things to try and the problem might or might not start before your copy commend, it might be the copy command, or it might be something else - could even be something as silly as a spelling mistake (and we all do that).

 

It might be easier that way than trying to debug this one comment at a time.. and perhaps a small sample of a drawing so we can see what you are trying to copy. From what you say I suspect you aren't quite getting the points out of your lists but.... could do with seeing how the list is made up to give you a clue of what to do next.. and that might then go back to how the list is constructed in the first place - a while in the code before the copy command

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