Align UCS to segment pline in block or xref ?

Align UCS to segment pline in block or xref ?

Anonymous
Not applicable
1,732 Views
11 Replies
Message 1 of 12

Align UCS to segment pline in block or xref ?

Anonymous
Not applicable

hi all

 

I found lisp:

 

(defun C:UCS3 ()
(setq ent_name (car (nentsel)))
(setq copy_of_ent (entmake (entget ent_name)))
(command "._UCS" "_Entity" (entlast))
 (entdel (entlast))
(princ)
)

 

But ucs align to start and end point. I mean align to any segment selected. Pls see file attached

 

how can fix it ?

 

Thank you.

0 Likes
Accepted solutions (1)
1,733 Views
11 Replies
Replies (11)
Message 2 of 12

phanaem
Collaborator
Collaborator

This works on every curve, even 3D rotated.

For non-planar curves, the Y direction is calculated accordingly to Arbitrary Axis Algorithm

;Set UCS aligned to segment
;Stefan M. - 05.08.2016
(defun c:ucsobj ( / vxs vxv mod unv v*v msg e p x y)
  
  (defun vxs (v s) (mapcar '(lambda (a) (* a s)) v))   ;scale vector
  (defun vxv (u v) (apply '+ (mapcar '* u v)))         ;dot_product
  (defun mod (v) (sqrt (vxv v v)))                     ;vector's module
  (defun unv (v) (vxs v (/ 1. (mod v))))               ;unit vector
  (defun v*v (a b)                                     ;cross product
    (list
      (- (* (cadr  a) (caddr b)) (* (caddr a) (cadr  b)))
      (- (* (caddr a) (car   b)) (* (car   a) (caddr b)))
      (- (* (car   a) (cadr  b)) (* (cadr  a) (car   b)))
    )
  )
  
  (if
    (and
      (setq msg "\nNothing selected.")
      (setq e (entsel))
      (setq p (osnap (cadr e) "nea"))
      (setq msg "\nInvalid object.")
      (wcmatch (cdr (assoc 0 (entget (car e)))) "LINE,SPLINE,CIRCLE,ARC,ELLIPSE,*POLYLINE")
    )
    (progn
      (setq e (car  e)
            p (trans p 1 0)
            x (unv (vlax-curve-getfirstderiv e (vlax-curve-getparamatpoint e p)))   
      )
      (if
        (vlax-curve-isplanar e)
        (setq y (v*v (cdr (assoc 210 (entget e))) x))
        (setq y (v*v (if (< (car x) (/ 1.0 64.0)) '(0.0 1.0 0.0) '(0.0 0.0 1.0)) x))
        )
      (command "_ucs" "_3p"
               "_non" (trans            p    0 1)
               "_non" (trans (mapcar '+ p x) 0 1)
               "_non" (trans (mapcar '+ p y) 0 1)
               )
      )
    (princ msg)
    )
  (princ)
  )
0 Likes
Message 3 of 12

ВeekeeCZ
Consultant
Consultant

Maybe using NCOPY?

 

(defun C:UCS3 ( / pt)
  (command "_.NCOPY" PAUSE)
  (setq pt (getvar 'lastpoint))
  (command "" "_D" '(0 0 0)
	   "_.UCS" "_Entity" (list (entlast) pt))
  (entdel (entlast))
(princ)
)

 

0 Likes
Message 4 of 12

scot-65
Advisor
Advisor
There is yet another method that does not show in the
options list of the command UCS: 3P (3 point or just 3).

>> Command: UCS
>> Current ucs name: *WORLD*
>> Specify origin of UCS or [Face/NAmed/OBject/Previous/View/World/X/Y/Z/ZAxis] <World>: 3

Select 2 points which represents the positive x-axis and
[Enter] for the third.

To restore, command UCS "World".

I have quick keys "UR" and "UW" to assist me.
Both are LISP and not a PGP item.

Inside these keys I turn the UCS icon On when rotating and
UCS icon Off when returning to world (2d drafting here).

???

Scot-65
A gift of extraordinary Common Sense does not require an Acronym Suffix to be added to my given name.

0 Likes
Message 5 of 12

Anonymous
Not applicable

 Hi phanaem,

 

I mean align ucs to segment pline in block. Your code use entsel then select only block.

 

Thanks for your solution.

0 Likes
Message 6 of 12

Anonymous
Not applicable
hi BeekeeCZ ^_^

NCOPY is command express, so i cannot call line (command "_.NCOPY" PAUSE)
how can fix it.

Thanks
0 Likes
Message 7 of 12

Anonymous
Not applicable

Hi Scot,

 

I mean use single pick to align. It is same way "ucs" "obj" all object. 

 

Thanks.

0 Likes
Message 8 of 12

Kent1Cooper
Consultant
Consultant

Since your example appears to be a Polyline, you may be able to modify AlignSnap.lsp with its AS command, available here.  It rotates the SNAPANG System Variable setting, rather than changing the UCS, but if that isn't enough for you, it should be easy enough to modify to do the latter instead.  It doesn't do nested entities [such as Lines within Blocks] that the (nentsel) in your example code would find.  However, a segment in a Polyline is not a "nested" entity, but part of the "main" entity, so maybe it will serve your purpose.

 

Never mind -- I see now from Post 5 that you are looking for nested entities.  Here's a question:  Are they always going to be straight ones?  You ought to be able to do something involving (osnap) functions applying the MID and END object snap modes at the point where you pick.

Kent Cooper, AIA
0 Likes
Message 9 of 12

Kent1Cooper
Consultant
Consultant
Accepted solution

@Kent1Cooper wrote:

....  Are they always going to be straight ones?  You ought to be able to do something involving (osnap) functions applying the MID and END object snap modes at the point where you pick.


Such as this [minimally tested]:

(defun C:UCSN (/ esel pt)
  (if (setq esel (entsel "\nSelect straight object/nested object to align UCS with: "))
    (progn
      (setq pt (trans (cadr esel) 1 0)); put into World Coordinates
      (command "_.ucs" (osnap (trans pt 0 1) "_endpoint") (osnap (trans pt 0 1) "_midpoint") "")
    ); progn
  ); if
  (princ)
); defun

It was a little trickier than I expected -- apparently once you've given it an origin point, it reads the along-the-X-axis point you give it in relation to that new origin, which is why the routine needs to store the pick point as a WCS location, and (trans)late it into current coordinates each time.

 

 

It accepts the default on the point on the XY plane, since there are infinite possible UCS's otherwise.  But it works on either top-level or nested straight things of any variety that has mid- and endpoints to which it can Osnap.

Kent Cooper, AIA
Message 10 of 12

Anonymous
Not applicable

Very nice Kent. Smiley Very Happy

 

 

Ha ha, okey.

 

Thank you so much.

0 Likes
Message 11 of 12

scot-65
Advisor
Advisor
>>It was a little trickier than I expected

endpoint / midpoint method.
didn't think of that.
🙂

Scot-65
A gift of extraordinary Common Sense does not require an Acronym Suffix to be added to my given name.

0 Likes
Message 12 of 12

Kent1Cooper
Consultant
Consultant

@scot-65 wrote:
....
endpoint / midpoint method.
didn't think of that.
🙂

It could also use ENDpoint and NEArest, on the assumption that you're not likely to pick such a thing precisely at an endpoint and therefore get the same result from both Osnaps; or it could use MID and NEA if you don't pick it precisely at the midpoint [which seems slightly more possible, especially with Snap on].

 

But I am reminded of a possible issue:

 

If you would ever want to pick an Xline or Ray to align with, the routine would need to be built a little differently, because Xlines don't have ENDpoints and Rays don't have MIDpoints.  If the selected object is an Xline, it should use MID and NEA; if it's a Ray, it should use END and NEA.  It would count on your not picking either precisely at its origin point [which is what MID snaps to on an Xline, and END on a Ray].

Kent Cooper, AIA
0 Likes