LISP INSERT ARW

LISP INSERT ARW

smallƑish
Advocate Advocate
1,070 Views
10 Replies
Message 1 of 11

LISP INSERT ARW

smallƑish
Advocate
Advocate

 

Need help to fic the code, Just insert "D_ARW" Block in each Mline midle. 

 

(defun c:IN_ARW (/ mline-list mline start-point end-point ARW-block mid-point i)
  (setq mline-list (ssget '((0 . "MLINE"))))
  (setq ARW-block "D_ARW") ; Block name

  (if mline-list
    (progn
      (setq i 0)
      (repeat (sslength mline-list)
        (setq mline (ssname mline-list i))
        (setq start-point (vlax-curve-getStartPoint mline))
        (setq end-point (vlax-curve-getEndPoint mline))
        
        (setq mid-point (list (/ (+ (car start-point) (car end-point)) 2.0)
                              (/ (+ (cadr start-point) (cadr end-point)) 2.0)))
        
        (command "_.INSERT" ARW-block mid-point 1 1 0) ; Insert block with correct rotation
        (setq i (1+ i)) ; Increment index
      )
    )
  )
  (princ)
)

 

0 Likes
Accepted solutions (2)
1,071 Views
10 Replies
Replies (10)
Message 2 of 11

vladimir_michl
Advisor
Advisor
Accepted solution

If your MLINEs are really just those single-segment types, you can use something line this:

(defun c:IN_ARW (/ mline-list mline start-point end-point ARW-block mid-point i coords)
  (setq mline-list (ssget '((0 . "MLINE"))))
  (setq ARW-block "D_ARW") ; Block name

  (if mline-list
    (progn
      (setq i 0)
      (repeat (sslength mline-list)
        (setq mline (ssname mline-list i))
        (setq coords (vlax-safearray->list (vlax-variant-value (vla-get-coordinates (vlax-ename->vla-object mline)))))
        ;(setq start-point (vlax-curve-getStartPoint mline))
        ;(setq end-point (vlax-curve-getEndPoint mline))
        (setq start-point (list (nth 0 coords) (nth 1 coords))) ; 2D
        (setq end-point (list (nth 3 coords) (nth 4 coords)))
        (setq mid-point (list (/ (+ (car start-point) (car end-point)) 2.0)
                              (/ (+ (cadr start-point) (cadr end-point)) 2.0)))
        
        (command "_.INSERT" ARW-block mid-point 1 1 (angtos (angle start-point end-point))) ; Insert block with correct rotation
        (setq i (1+ i)) ; Increment index
      )
    )
  )
  (princ)
)

 

Vladimir Michl, www.arkance.world  -  www.cadforum.cz

 

Vladimír Michl, CADs.cz - LinkedIn

0 Likes
Message 3 of 11

smallƑish
Advocate
Advocate

Thank you so much 

0 Likes
Message 4 of 11

Moshe-A
Mentor
Mentor
Accepted solution

@smallƑish hi,

 

i see you are fast catching AutoLISP and this is very nice (as president Trump saids 😀)

you declared all variables as local and this is very good 👍

 

two things i want to enlighten:

the first, when using (command) function be aware the user has running object snap, this may interfere with your lisp resulting unwanted outcome. in this case if user has endpoint running, your block would be inserted at the close endpoint of mline. to overcome this add a "None" osnap before the point arguments

 

(command "._insert" ARW-BLOCK "_none" mid-point 1 1 0)

 

at start disable command echoing => (setvar "cmdecho" 0)

next invoke (command "._undo" "_begin")

before exist (command "._undo" "_end")

at exist (setvar "cmdecho" 1)

 

the undo calls enable the user to undo the lisp work with one U

 

second things

is finding the middle of two points:

 

what you did is perfectly all right (for beginner) but you want to advance - yes?!

so here is a nice function called (midpt) using the magnificent (mapcar) (lambda) functions

the idea here is the same as you did, addition two points divided by 2

this works if points are 2d or 3d.

 

enjoy

Moshe

 

 

 

(defun midpt (p0 p1 / x0 x1)
 (mapcar
   '(lambda (x0 x1)
      (/ (+ x0 x1) 2)
    )
  p0 p1 
 )
); midpt

 

 

 

 

Message 5 of 11

smallƑish
Advocate
Advocate

@Moshe-A  

 

Thank you so much for your advice, which I consider as guidance from a godfather. "None" OSNAP—I was looking for that, and you spotted it immediately! Also, all the additional knowledge is greatly appreciated. Thanks again!

0 Likes
Message 6 of 11

Sea-Haven
Mentor
Mentor

A couple more examples,

(setq mp (mapcar '* (mapcar '+ p1 p3) '(0.5 0.5)))
(setq mp (mapcar '/ (mapcar '+ (vlax-curve-getStartPoint lin) (vlax-curve-getEndPoint lin)) '(2 2 2)))

For a pline with multi segments the mid point can be found by taking the length of the Pline and divide by 2 then use .

(setq mp (vlax-curve-getpointatdist obj dist))

 

0 Likes
Message 7 of 11

smallƑish
Advocate
Advocate

how can we make the insertion on a block . instead of mline ?

sample file attached.

0 Likes
Message 8 of 11

vladimir_michl
Advisor
Advisor

This will be quite difficult, especially with the "dynamic" nature of dynamic blocks. Have you considered including the symbol directly in your block? Then it can behave dynamically, staying in the middle even after changing the segment length.

 

Vladimir Michl, www.arkance.world  -  www.cadforum.cz

 

Vladimír Michl, CADs.cz - LinkedIn

0 Likes
Message 9 of 11

Moshe-A
Mentor
Mentor

@smallƑish ,

 

Maybe this:

if you define your pipe block to be dynamic change in one direction (length) and you keep the insertion point of the two to form the intersection, then this can be done very easily.

 

Moshe

 

Message 10 of 11

smallƑish
Advocate
Advocate

Can that possible to explode the block and pick the middle point then save the mid point data. then undo all and insert arow on the stored point? 

looks like crazy thoughts, but still feel a door is open that way. 🙄

0 Likes
Message 11 of 11

vladimir_michl
Advisor
Advisor

How about this? Using just dynamic properties - the symbol stays always centered and it is a part of the block. You may want to scale it up a little.

 

Vladimir Michl, www.arkance.world  -  www.cadforum.cz

 

Vladimír Michl, CADs.cz - LinkedIn

0 Likes