Jump to content

More efficient way to write specified code?


BHAInc

Recommended Posts

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

Link to comment
Share on other sites

(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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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