Jump to content

LISP to Copy External Reference Saved Path to MText Field


hxbtdtna

Recommended Posts

Hi all,

Can someone help me to macke a LISP to copy the External Reference Saved Path to Mtext Field.

I try some to make this LISP and get the ObjectID from Lee Mac but something went wrong:

 

(defun c:xpa (/ xObj fldObj xID util fldtxt)

(vl-load-com)

 

;; ObjectID - Lee Mac

;; Returns a string containing the ObjectID of a supplied VLA-Object

;; Compatible with 32-bit & 64-bit systems

 

(defun LM:objectid ( obj )

(eval

(list 'defun 'LM:objectid '( obj )

(if (wcmatch (getenv "PROCESSOR_ARCHITECTURE") "*64*")

(if (vlax-method-applicable-p (vla-get-utility (LM:acdoc)) 'getobjectidstring)

(list 'vla-getobjectidstring (vla-get-utility (LM:acdoc)) 'obj ':vlax-false)

'(LM:ename->objectid (vlax-vla-object->ename obj))

)

'(itoa (vla-get-objectid obj))

)

)

)

(LM:objectid obj)

)

 

;; Entity Name to ObjectID - Lee Mac

;; Returns the 32-bit or 64-bit ObjectID for a supplied entity name

 

(defun LM:ename->objectid ( ent )

(LM:hex->decstr

(setq ent (vl-string-right-trim ">" (vl-prin1-to-string ent))

ent (substr ent (+ (vl-string-position 58 ent) 3))

)

)

)

;; Hex to Decimal String - Lee Mac

;; Returns the decimal representation of a supplied hexadecimal string

 

(defun LM:hex->decstr ( hex / foo bar )

(defun foo ( lst rtn )

(if lst

(foo (cdr lst) (bar (- (car lst) (if (

(apply 'strcat (mapcar 'itoa (reverse rtn)))

)

)

(defun bar ( int lst )

(if lst

(if (or (

(cons (rem int 10) (bar (/ int 10) (cdr lst)))

)

(bar int '(0))

)

)

(foo (vl-string->list (strcase hex)) nil)

)

 

(setq xObj (LM:ename->objectid

(car (nentsel "\nSelect the xref: "))

)

fldObj (vlax-ename->vla-object (car (nentsel "\nSelect the Field: ")))

);;

;; if

(setq fldtxt (strcat "%

xobj

">%).path>%"

)

)

(vla-put-textstring fldObj fldtxt)

(vla-update fldObj)

(vla-regen (vla-get-activedocument (vlax-get-acad-object))

1

)

(princ)

);; xpa

 

Thank you.

Link to comment
Share on other sites

the problems I've met that I can only select the line inside the xref, not the whole xref as a block.

And I need the xref saved path can be selected when I work on Layout space.

It maybe similar with "Field update customscale viewport" lisp.

Link to comment
Share on other sites

Hi,

 

Try this:

(defun c:path2fldtxt (/ xref doc p o id u)
 ;;------------------------------------;;
 ;; Tharwat - Date: 21.Mar.2017	;;
 ;; Write external reference saved path;;
 ;; to field text.			;;
 ;;------------------------------------;;
 (and (princ "\nPick on External Reference :")
      (setq xref (ssget "_+.:S:E" '((0 . "INSERT"))))
      (/= ""
          (cdr
            (assoc 1
                   (entget (tblobjname
                             "BLOCK"
                             (cdr (assoc 2 (entget (ssname xref 0))))
                           )
                   )
            )
          )
      )
      (setq doc (vla-get-activedocument (vlax-get-acad-object))
            p   (getpoint "\nSpecify a point :")
      )
      (setq o  (vlax-ename->vla-object (ssname xref 0))
            id (if (vlax-method-applicable-p
                     (setq u (vla-get-utility doc))
                     'getobjectidstring
                   )
                 (vla-getobjectidstring u o :vlax-false)
                 (itoa (vla-get-objectid o))
               )
      )
      (vla-addtext
        (vla-get-block (vla-get-activelayout doc))
        (strcat "%<\\AcObjProp Object(%<\\_ObjId " id ">%).Path>%")
        (vlax-3d-point p)
        (getvar 'textsize)
      )
 )
 (princ)
)(vl-load-com)

Link to comment
Share on other sites

Thank you Tharwat, but I want to make the selection of xref saved path in Layout Space.

Could you make it like "Field update customscale viewport" lisp below. In this lisp you can choose the viewport and replace the text field with the current scale.

 

(defun c:vps (/ vpObj fldObj vpID util fldtxt)

(vl-load-com)

(setq vpObj (vlax-ename->vla-object

(car (nentsel "\nSelect the viewport: "))

)

fldObj (vlax-ename->vla-object (car (nentsel "\nSelect the Field: ")))

vpID (vla-get-objectid vpObj)

);; setq

(if (> (vl-string-search "x64" (getvar "platform")) 0)

(progn

(setq util (vla-get-Utility

(vla-get-activedocument (vlax-get-acad-object))

)

vpID (vlax-invoke-method

util

"GetObjectIdString"

vpObj

:vlax-False

)

);; setq

);; progn

(setq vpID (vl-princ-to-string (vla-get-Objectid vpObj)))

);; if

(setq fldtxt (strcat "%

vpID

">%).CustomScale \\f \"%sn\">%"

)

)

(vla-put-textstring fldObj fldtxt)

(vla-update fldObj)

(vla-regen (vla-get-activedocument (vlax-get-acad-object))

1

)

(princ)

);; vps

 

Thank you for your help.

Link to comment
Share on other sites

When I chose Insert Field, go to Object and you can select the Xref in Viewport in Layout Space and choose the saved path.

So, I want to make a lisp to automate this action, and replace it to the text field in Dynamic Block.

Link to comment
Share on other sites

Try this quickly written. :)

 

(defun c:path2fldtxt (/ *error* dlg id f st v l lst rtn e o id u ss sn
                     bn nst obj doc)
 ;;------------------------------------;;
 ;; Tharwat - Date: 21.Mar.2017	;;
 ;;------------------------------------;;
 (defun *error* (msg)
   (and dlg (findfile dlg) (vl-file-delete dlg))
   (if (and msg
            (not (wcmatch (strcase msg) "*BREAK*,*CANCEL*,*EXIT*"))
       )
     (princ (strcat "\nError => " msg))
   )
   (princ)
 )
 (cond
   ((not
      (and
        (setq ss (ssget "_X" '((0 . "INSERT"))))
        (progn
          (while (setq sn (ssname ss 0))
            (if
              (/=
                ""
                (cdr
                  (assoc 1
                         (entget (tblobjname
                                   "BLOCK"
                                   (setq bn (cdr (assoc 2 (entget sn))))
                                 )
                         )
                  )
                )
              )
               (setq lst (cons (list bn sn) lst))
            )
            (ssdel sn ss)
          )
          lst
        )
        (setq dlg (vl-filename-mktemp nil nil ".dcl"))
        (setq f (open dlg "w"))
        (write-line
          "test : dialog { label = \"External References\"; width = 32; fixed_width = true;
          spacer_1;
          : list_box { key = \"names\"; }
          spacer_1;
          : row {
          : button { label = \"Okay\"; key = \"oki\";}
          : button { label = \"Exit\"; key = \"esc\"; is_default = true; is_cancel = true;}}}"
          f
        )
        (not (close f))
      )
    )
    (alert "Can't load the temporary file <!>")
   )
   ((or (not dlg)
        (not (> (setq id (load_dialog dlg)) 0))
        (not (new_dialog "test" id)
        )
    )
    (princ "\nCan not load Dialog !")
   )
   (t
    (start_list "names")
    (mapcar 'add_list (mapcar 'car lst))
    (end_list)
    (set_tile "names" "0")
    (action_tile
      "oki"
      "(setq rtn (get_tile \"names\"))(done_dialog)"
    )
    (action_tile "esc" "(setq rtn nil) (done_dialog)")
    (start_dialog)
    (unload_dialog id)
    (vl-file-delete dlg)
   )
 )
 (if (and rtn
          (setq nst
                 (car
                   (nentsel
                     "\nPick on a <Text,Mtext,Attribute> to replace with a field path :"
                   )
                 )
          )
          (wcmatch (cdr (assoc 0 (entget nst))) "ATTRIB,TEXT,MTEXT")
          (vlax-write-enabled-p
            (setq obj (vlax-ename->vla-object nst))
          )
          (setq doc (vla-get-activedocument (vlax-get-acad-object)))
     )
   (progn
     (vla-put-textstring obj "")
     (vla-put-textstring
       obj
       (strcat "%<\\AcObjProp Object(%<\\_ObjId "
               (setq o  (vlax-ename->vla-object
                          (cadr (assoc (nth (atoi rtn) (mapcar 'car lst)) lst))
                        )
                     id (if (vlax-method-applicable-p
                              (setq u (vla-get-utility doc))
                              'getobjectidstring
                            )
                          (vla-getobjectidstring u o :vlax-false)
                          (itoa (vla-get-objectid o))
                        )
               )
               ">%).Path>%"
       )
     )
     (vla-regen doc acactiveviewport)
   )
 )
 (princ)
)(vl-load-com)

Link to comment
Share on other sites

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