Jump to content

Recommended Posts

Posted

I’m trying to locate the closest point to selection point on a line contained in a block (the only one item in that block); that it, I’m looking for a point that lays on that entity. By using code below seems that the line is recognized as misplaced?!?

 

(setq UserSelection (nentsel "\nSelect line (in block): ")
     LineFromBlock  (car  UserSelection)
     SelectionPoint (cadr UserSelection))

(setq ClosestPoint (vlax-curve-getClosestPointTo (vlax-ename->vla-object LineFromBlock)
                                                SelectionPoint
                                                T))
(command "CIRCLE" ClosestPoint 1.0)   ;DEBUG ONLY

 

Can someone help solve my issue? Thank you!

 

Regards,

Posted

Try this

(setq UserSelection (nentsel "\nSelect line (in block): ")
     LineFromBlock  (car  UserSelection)
     SelectionPoint (OSNAP(cadr UserSelection) "NEA"))


(command "CIRCLE" SelectionPoint 1.0)   ;DEBUG ONLY

Posted

Yes, the point lays on line now. Thank you very much!

 

Have done a lot of tests with my code trying to understand the result and its strange how the debug points tend to align to a fictive line.

Posted

Can't answer for sure but it may have something to do with the difference with the UCS and ECS (Entity Coordinate System, which blocks use for definition of the entities within)

But I could be wrong...

Posted

Already tried to convert the point to/from Entity Coordinates System (used entity name as argument for TRANS function) but no luck…

Posted

I don't think TRANS will work directly for the conversion; a transformation matrix is needed. I'm sure someone else on this forum could explain/demonstrate the method better than I, I've just read about it.

Posted

Msasu,

 

For a nested object, you will need to use the transformation matrix returned by nentsel to get the correct points.

 

Will post an example in a bit. :)

Posted

I figured post #6 would get you involved...

Posted

Hehe

 

Something like this Msasu:

 

(defun GetClosestNested (/ ent cPt Matr pt)
 (vl-load-com)

 (if (and (setq ent (nentselp "\nSelect Line in Block: "))
          (setq cPt (getpoint "\nSelect Point to Test: ")))
   (progn

     (setq Matr (caddr ent)
           ent  (entmakex (append (entget (car ent)) '((60 . 1)))))

     (if Matr (vla-transformby
                (vlax-ename->vla-object ent) (vlax-tmatrix Matr)))

     (setq pt (vlax-curve-getClosestPointto ent cPt))
     
     (entdel ent)))
 
 pt)



(defun c:test (/ p)

 (if (setq p (GetClosestNested))
   (entmakex (list (cons 0 "POINT") (cons 10 p))))

 (princ))

Posted

@lpseifert, @Lee Mac and @alanjt - Thank you very much for your solutions and examples!

Posted
@lpseifert, @Lee Mac and @alanjt - Thank you very much for your solutions and examples!

Hope it helped. :)

Posted

You're welcome Msasu, happy to help. Its a shame a copy must be created, as this can be a problem if used within a loop but, if made invisible (as in my example), you needn't delete it til after the loop, meaning the routine is less CPU intensive.

Posted

I went the long way, works with non-uniformly scaled blocks, rotated blocks, non-world ucs:

 

 

;;-------------------------------------------------------------------------------------------
;;  SAMPLE ROUTINE TO TRANSFORM POINT FROM/TO BLOCK COORDINATE/UCS
;;  
(setq UserSelection  (nentsel "\nSelect line (in block): ")
     LineFromBlock  (car UserSelection)
     SelectionPoint (cadr UserSelection)
     blockinspoint  (cdr (assoc 10 (entget (car (last UserSelection)))))
     blockscale_x   (cdr (assoc 41 (entget (car (last UserSelection)))))
     blockscale_y   (cdr (assoc 42 (entget (car (last UserSelection)))))
     blockscale_z   (cdr (assoc 43 (entget (car (last UserSelection)))))
     blockrotation  (cdr (assoc 50 (entget (car (last UserSelection)))))
)
;;
;;
;;
(setq ClosestPoint
        (vlax-curve-getClosestPointTo
            LineFromBlock
            ;;
            ;;
            ;;  TRANSFORM SELECTIONPOINT COORDINATE FROM
            ;;  USER COORDINATE SYSTEM TO
            ;;  BLOCK COORDINATE SYSTEM
            ;;
            ;;  UCS->WORLD => MOVE => ROTATE => SCALE
            ;;
            ;;
            (mapcar '/
                    (polar '(0 0 0)
                           (- (angle '(0 0 0)
                                     (mapcar '-
                                             (trans SelectionPoint 1 0)
                                             (polar '(0 0 0)
                                                    (angle '(0 0 0) blockinspoint)
                                                    (distance '(0 0 0) blockinspoint)
                                             )
                                     )
                              )
                              blockrotation
                           )
                           (distance '(0 0 0)
                                     (mapcar '-
                                             (trans SelectionPoint 1 0)
                                             (polar '(0 0 0)
                                                    (angle '(0 0 0) blockinspoint)
                                                    (distance '(0 0 0) blockinspoint)
                                             )
                                     )
                           )
                    )
                    (list blockscale_x blockscale_y blockscale_z)
            )
        )
)
;;
;;
;;
(setq ClosestPoint
        ;;
        ;;
        ;;  TRANSFORM CLOSESTPOINT FROM
        ;;  BLOCK COORDINATE SYSTEM
        ;;  TO USER COORDINATE SYTEM
        ;;
        ;;  SCALE => ROTATE => MOVE => WORLD->UCS
        ;;
        ;;
        (trans
            (mapcar '+
                    (polar '(0 0 0)
                           (+ (angle '(0 0 0)
                                     (mapcar '*
                                             ClosestPoint
                                             (list blockscale_x blockscale_y blockscale_z)
                                     )
                              )
                              blockrotation
                           )
                           (distance '(0 0 0)
                                     (mapcar '*
                                             ClosestPoint
                                             (list blockscale_x blockscale_y blockscale_z)
                                     )
                           )
                    )

                    (polar '(0 0 0)
                           (angle '(0 0 0) blockinspoint)
                           (distance '(0 0 0) blockinspoint)
                    )
            )
            0
            1
        )
)
;;
;;
(command "CIRCLE" ClosestPoint 1.0)    
;;
;;WIZ_20MAR10--------------------------------------------------------------------------------

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