TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Woabow on January 31, 2013, 10:02:33 AM

Title: Setting UCS according a (3D rotated) block
Post by: Woabow on January 31, 2013, 10:02:33 AM
After spending some time to solve it myself I'm feeling close, but I see a lot of matrix calculations which I do not understand completely.

My problem: I just want to create a named UCS with Visual lisp code like this, where the UCS follows the OCS of a  selected block

Code: [Select]
(while (not (and
           (setq nsel (entsel "\nSelect Block: "))           
           (= (cdr (assoc 0 (entget (setq ent (car nsel))))) "INSERT")
       ))
)

    (setq vlobj (vlax-ename->vla-object ent))
    (setq insertion (vlax-get vlobj 'InsertionPoint))
    (setq no (vlax-get vlobj 'Normal))
    (setq rotation (vlax-get vlobj 'Rotation))


(setq ocs (vla-add (vla-get-UserCoordinateSystems (vla-get-activedocument (vlax-get-acad-object))
                          (vlax-3d-Point '(insertion?))
                          (vlax-3d-Point '(? ? ?))
                      (vlax-3d-Point '(? ? ?))
                     "SCOCS"
                    )
)



Title: Re: Setting UCS according a (3D rotated) block
Post by: Lee Mac on January 31, 2013, 10:21:07 AM
Maybe:

Code: [Select]
(defun c:bucs ( / e i n o z )
    (if (setq e (ssget "_+.:E:S" '((0 . "INSERT"))))
        (progn
            (setq e (entget (ssname e 0))
                  z (cdr (assoc 210 e))
                  o (cdr (assoc 010 e))
                  i 0
            )
            (while (tblsearch "UCS" (setq n (strcat "BUCS" (itoa (setq i (1+ i)))))))
            (vlax-invoke
                (vla-get-usercoordinatesystems (vla-get-activedocument (vlax-get-acad-object)))
                'add
                (trans o z 0)
                (trans (mapcar '+ o '(1.0 0.0 0.0)) z 0)
                (trans (mapcar '+ o '(0.0 1.0 0.0)) z 0)
                n
            )
        )
    )
    (princ)
)
Title: Re: Setting UCS according a (3D rotated) block
Post by: Woabow on January 31, 2013, 10:44:34 AM
Thanks Lee, I was about the same point as you, with less nice code, that is...

But the UCS does not follow the rotation of the block, see attached DWG

If I do the "UCS" command with "Object" option it does follow the rotation like I would like it to do.
Title: Re: Setting UCS according a (3D rotated) block
Post by: Lee Mac on January 31, 2013, 11:16:59 AM
Perhaps:
Code: [Select]
(defun c:bucs ( / e i n o r z )
    (if (setq e (ssget "_+.:E:S" '((0 . "INSERT"))))
        (progn
            (setq e (entget (ssname e 0))
                  z (cdr (assoc 210 e))
                  o (cdr (assoc 010 e))
                  r (cdr (assoc 050 e))
                  i 0
            )
            (while (tblsearch "UCS" (setq n (strcat "BUCS" (itoa (setq i (1+ i)))))))
            (vlax-invoke
                (vla-get-usercoordinatesystems (vla-get-activedocument (vlax-get-acad-object)))
                'add
                (trans o z 0)
                (trans (polar o r 1.0) z 0)
                (trans (polar o (+ r (/ pi 2.0)) 1.0) z 0)
                n
            )
        )
    )
    (princ)
)
Title: Re: Setting UCS according a (3D rotated) block
Post by: Woabow on January 31, 2013, 12:51:25 PM
This does the trick, many thanks (again :-)).

Title: Re: Setting UCS according a (3D rotated) block
Post by: Lee Mac on January 31, 2013, 01:35:34 PM
This does the trick, many thanks (again :-)).

You're welcome Woabow  :-)
Title: Re: Setting UCS according a (3D rotated) block
Post by: fixo on January 31, 2013, 04:29:04 PM
Hi Lee Mac
Do you have an idea how to set UCS
on face of 3d solid like box?
(then the Z axis will be perpendicularly to it)?
Kind regards,
Oleg
Title: Re: Setting UCS according a (3D rotated) block
Post by: Lee Mac on January 31, 2013, 08:24:21 PM
Hi Lee Mac
Do you have an idea how to set UCS
on face of 3d solid like box?
(then the Z axis will be perpendicularly to it)?
Kind regards,
Oleg

Hi Fixo,
Unfortunately, I don't know how to determine the normal vector of a selected face, or even how to determine the selected face of a 3D Solid since it seems that solids are not entirely exposed to AutoLISP / Visual LISP.  :|
Title: Re: Setting UCS according a (3D rotated) block
Post by: gile on February 01, 2013, 12:29:02 AM
Hi oleg,

You can look at the Autodesk.AutoCAD.BoundaryRepresentation.Brep .NET class to access to 3d solid datas.

I tried something with a.NET defined LISP function here:
http://www.theswamp.org/index.php?topic=39278.msg445148#msg445148
Title: Re: Setting UCS according a (3D rotated) block
Post by: fixo on February 01, 2013, 01:33:40 AM
Thanks Gilles, I know this way but I need to do it using lisp
Kind regards,
Oltg