Jump to content
BHAInc

More efficient way to write specified code?

Recommended Posts

BHAInc

Is there a more efficient way to write the following code?

Thanks in advance for any suggestions.

(mapcar
   (function
       (lambda (l) (vl-catch-all-apply 'vla-addline l))
   )
   (list
       (list (vla-get-paperspace (acdoc))
      (vlax-3d-point '(0.5 1.0 0.0))
      (vlax-3d-point '(8.0 1.0 0.0))
)
       (list (vla-get-paperspace (acdoc))
      (vlax-3d-point '(0.0 0.0 0.0))
      (vlax-3d-point '(8.5 0.0 0.0))
)
       (list (vla-get-paperspace (acdoc))
      (vlax-3d-point '(0.0 11.0 0.0))
      (vlax-3d-point '(0.0 0.0 0.0))
)
       (list (vla-get-paperspace (acdoc))
      (vlax-3d-point '(0.5 10.5 0.0))
      (vlax-3d-point '(0.5 1.0 0.0))
)
       (list (vla-get-paperspace (acdoc))
      (vlax-3d-point '(8.0 1.0 0.0))
      (vlax-3d-point '(8.0 10.5 0.0))
)
       (list (vla-get-paperspace (acdoc))
      (vlax-3d-point '(8.5 0.0 0.0))
      (vlax-3d-point '(8.5 11.0 0.0))
)
       (list (vla-get-paperspace (acdoc))
      (vlax-3d-point '(0.5 10.5 0.0))
      (vlax-3d-point '(8.0 10.5 0.0))
)
       (list (vla-get-paperspace (acdoc))
      (vlax-3d-point '(0.0 11.0 0.0))
      (vlax-3d-point '(8.5 11.0 0.0))
)
   )
)

Share this post


Link to post
Share on other sites
Grrr

(mapcar 
 '(lambda (x) (entmakex (list '(0 . "LINE") (cons 10 (car x)) (cons 11 (cadr x)))))
   '(
     ((0.5 1.0 0.0) (8.0 1.0 0.0))
     ((0.0 0.0 0.0) (8.5 0.0 0.0))
     ((0.0 11.0 0.0) (0.0 0.0 0.0))
     ((0.5 10.5 0.0) (0.5 1.0 0.0))
     ((8.0 1.0 0.0) (8.0 10.5 0.0))
     ((8.5 0.0 0.0) (8.5 11.0 0.0))
     ((0.5 10.5 0.0) (8.0 10.5 0.0))
     ((0.0 11.0 0.0) (8.5 11.0 0.0))
   )
 )
)

 

EDIT:

For 2D points (on zero elevation), maybe something like this:

(mapcar 
 '(lambda (x) (entmakex (mapcar 'cons '(0 10 11) (append '("LINE") (mapcar 'append x (mapcar 'list '(0. 0.)))))))
 '(
   ((0.5 1.0) (8.0 1.0))
   ((0.0 0.0) (8.5 0.0))
   ((0.0 11.0) (0.0 0.0))
   ((0.5 10.5) (0.5 1.0))
   ((8.0 1.0) (8.0 10.5))
   ((8.5 0.0) (8.5 11.0))
   ((0.5 10.5) (8.0 10.5))
   ((0.0 11.0) (8.5 11.0))
 )
)

HTH, I was bored.

Edited by Grrr

Share this post


Link to post
Share on other sites
BIGAL

Maybe make 1 big list ((pt1)(pt2)(pt1)(pt2).....) get length of the list then use repeat, Ok a question if its a 4 sided box just done twice why not use rectang 2 pts only required.

 

(setq lst  (list 
(list 0.5 1.0 0.0)
(list 8.0 1.0 0.0)
(list 0.0 0.0 0.0)
(list 8.5 0.0 0.0)
(list 0.0 11.0 0.0)
(list 0.0 0.0 0.0)
(list 0.5 10.5 0.0)
(list 0.5 1.0 0.0)
(list 8.0 1.0 0.0)
(list 8.0 10.5 0.0)
(list 8.5 0.0 0.0)
(list 8.5 11.0 0.0)
(list 0.5 10.5 0.0)
(list 8.0 10.5 0.0)
(list 0.0 11.0 0.0)
(list 8.5 11.0 0.0)
))
(setq x (length lst))
(repeat (/ (length lst) 2)
(command "line" (nth (- x 2) lst) (nth (- x 1) lst) "")
(setq x (- x 2))
)

 

my preference 5 lines long

(setq oldsnap (getvar "osmode"))
(setvar "osmode" 0)
(command "rectang" (list 0.5 1.0) (list 8.0 10.5))
(command "rectang" (list 0.0 0.0) (list 8.5 11.0))
(setvar "osmode" oldsnap)

Edited by BIGAL

Share this post


Link to post
Share on other sites
Lee Mac

I would suggest two lists:

(mapcar
  '(lambda ( a b ) (entmake (list '(0 . "LINE") (cons 10 a) (cons 11 b))))
  '((0.5 1.0)(0.0 0.0)(0.0 11.0)(0.5 10.5)(8.0  1.0)(8.5  0.0)(0.5 10.5)(0.0 11.0))
  '((8.0 1.0)(8.5 0.0)(0.0  0.0)(0.5  1.0)(8.0 10.5)(8.5 11.0)(8.0 10.5)(8.5 11.0))
)

Share this post


Link to post
Share on other sites
BHAInc

Excellent!

Thank you for all for the suggestions and replies, they're very helpful.

 

In the name of academic curiosity, is anyone able to suggest a more efficient way to write the code while using the "vla-addline" function?

 

Thanks in advance.

Share this post


Link to post
Share on other sites
marko_ribar

Copy+pasted lists from Lee...

 

(mapcar (function (lambda ( a b ) (vla-addline (vla-get-paperspace (vla-get-activedocument (vlax-get-acad-object))) (vlax-3d-point a) (vlax-3d-point b))))
  '((0.5 1.0)(0.0 0.0)(0.0 11.0)(0.5 10.5)(8.0  1.0)(8.5  0.0)(0.5 10.5)(0.0 11.0))
  '((8.0 1.0)(8.5 0.0)(0.0  0.0)(0.5  1.0)(8.0 10.5)(8.5 11.0)(8.0 10.5)(8.5 11.0))
)

Share this post


Link to post
Share on other sites
Lee Mac
Copy+pasted lists from Lee...

(mapcar (function (lambda ( a b ) (vla-addline (vla-get-paperspace (vla-get-activedocument (vlax-get-acad-object))) (vlax-3d-point a) (vlax-3d-point b))))
  '((0.5 1.0)(0.0 0.0)(0.0 11.0)(0.5 10.5)(8.0  1.0)(8.5  0.0)(0.5 10.5)(0.0 11.0))
  '((8.0 1.0)(8.5 0.0)(0.0  0.0)(0.5  1.0)(8.0 10.5)(8.5 11.0)(8.0 10.5)(8.5 11.0))
)

 

You realise that this is needlessly retrieving the Paperspace Container, Active Document Object and Application Object for every line created!?

 

These objects need only be obtained once and referenced locally, e.g.:

(   (lambda ( spc )
       (mapcar '(lambda ( a b ) (vlax-invoke spc 'addline a b))
          '((0.5 1.0 0.0)(0.0 0.0 0.0)(0.0 11.0 0.0)(0.5 10.5 0.0)(8.0  1.0 0.0)(8.5  0.0 0.0)(0.5 10.5 0.0)(0.0 11.0 0.0))
          '((8.0 1.0 0.0)(8.5 0.0 0.0)(0.0  0.0 0.0)(0.5  1.0 0.0)(8.0 10.5 0.0)(8.5 11.0 0.0)(8.0 10.5 0.0)(8.5 11.0 0.0))
       )
   )
   (vla-get-paperspace (vla-get-activedocument (vlax-get-acad-object)))
)

Share this post


Link to post
Share on other sites
Grrr

Also its a smart thing to avoid the point safearray conversion by using vlax-invoke, just like Lee did,

but for obtaining the container with model/paper space entities I think that this would be more suitable:

(or
 (setq 
   acDoc (vla-get-ActiveDocument (vlax-get-acad-object))
   AcSpc (vlax-get acDoc (if (= acModelSpace (vla-get-ActiveSpace acDoc)) 'ModelSpace (if (= (vla-get-mSpace acDoc) :vlax-true) 'ModelSpace 'PaperSpace)))
 )
 (vla-get-Block (vla-get-ActiveLayout (vla-get-ActiveDocument (vlax-get-acad-object))))
)

I mean either obtain the Mspace/Pspace by checking or directly access the block (I got used with the or funciton to write alternatives inside the code).

Share this post


Link to post
Share on other sites
BIGAL

I tried to find a vla-addrectang does it exist ?

 

vla-addLightweightPolyline does exist, so can pass the 4 points.

Share this post


Link to post
Share on other sites
Grrr
I tried to find a vla-addrectang does it exist ?

 

vla-addLightweightPolyline does exist, so can pass the 4 points.

 

Yeah, basically the result from the RECTANGLE command is a lwpolyline, i.e. when you dump a rectangle the first row is:

; IAcadLWPolyline: AutoCAD Lightweight Polyline Interface

just like entgetting the same thing should result (0 . "LWPOLYLINE"), so theres no such thing as rectangle method or entmaking an rectangle.

 

But you're leaving the idea for creating such subfunction that would add/emake rectangle lwpoly from a 2 points argument (lowerleft upperright).

 

EDIT: BTW heres another (with command calls) similar to yours:

(
 (lambda (c)
   (setvar 'cmdecho 0)
   (foreach x '(((0. 0. 0.) (8.5 11 0)) ((0.5 1.) (7.5 9.5 0)))
     (command "_.RECTANGLE" "_non" (car x) "_non" (mapcar '+ (car x) (cadr x)))
   )
   (setvar 'cmdecho c)
 )
 (getvar 'cmdecho)
)

Edited by Grrr

Share this post


Link to post
Share on other sites
BHAInc

Thank you again for all of the suggestions and replies. Very informative.

Share this post


Link to post
Share on other sites
BIGAL

Your right Grr I think the bottom line without continuing on is "do you want individual lines or rectangs" so both have been covered with different methods..

Share this post


Link to post
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
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

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