Author Topic: Locate closest point on a line from a block  (Read 2249 times)

0 Members and 1 Guest are viewing this topic.

msasu

  • Mosquito
  • Posts: 13
Locate closest point on a line from a block
« on: March 18, 2010, 07:27:45 AM »
I’m trying to locate the closest point to selection point on a line contained in a block; that it I’m looking for a point that lays on the entity. By using code blow seems that the line is recognized as misplaced?!?

Code: [Select]
(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,

gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: Locate closest point on a line from a block
« Reply #1 on: March 18, 2010, 08:45:27 AM »
Hi,

(car (nentsel)) or (car (nentselp)) returns the ename of the entity in the block definition, not in the block reference (insert).

Here's a way, which:
- copies the the nested entity from the bloc definition to the model space,
- transforms it with the nentselp transformation matrix (the reference have to be uniformly scaled),
- get the closest point,
- delete the entity copy.

Code: [Select]
;; GetClosestPointToNestedCurve
;; Returns the closest point to a nested curve (WCS coordinates)
;;
;; Arguments
;; nent = a list as returned by nentselp
;; pt =: a point (WCS coordinates)

(defun GetClosestPointToNestedCurve (nent pt / closestPoint)
  (setq tmp
(car
   (vlax-invoke
     *acdoc*
     'CopyObjects
     (list (vlax-ename->vla-object (car nent)))
     (vla-get-ModelSpace *acdoc*)
   )
)
  )
  (vla-TransformBy tmp (vlax-tmatrix (caddr nent)))
  (setq closestPoint
(vlax-curve-getClosestPointTo tmp pt)
  )
  (vla-Delete tmp)
  closestPoint
)

;; Tesing function

(defun c:test (/ pt nent closest)
  (vl-load-com)
  (or *acdoc*
      (setq *acdoc* (vla-get-ActiveDocument (vlax-get-acad-object)))
  )
  (if
    (and
      (setq pt (getpoint "\nSpecify a point: "))
      (setq nent (nentselp "\nSelect nested entity: "))
      (vlax-curve-getendParam (car nent))
    )
     (progn
       (setq closest
      (GetClosestPointToNestedCurve nent (trans pt 1 0))
       )
       (vla-addCircle
(vla-get-ModelSpace *acdoc*)
(vlax-3d-point closest)
1.0
       )
     )
  )
  (princ)
)
Speaking English as a French Frog

Lee Mac

  • Seagull
  • Posts: 12926
  • London, England
Re: Locate closest point on a line from a block
« Reply #2 on: March 18, 2010, 08:54:04 AM »
Nice one Gile, mine was also similar:

Code: [Select]
(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))

Joe Burke

  • Guest
Re: Locate closest point on a line from a block
« Reply #3 on: March 19, 2010, 09:22:00 AM »
This may be of interest: http://www.theswamp.org/index.php?topic=23170.60.

Check the last message posted for the latest version of MinDist.lsp.