Jump to content

Current drawing's path and filename in autolisp


Recommended Posts

mark_kostic

Hello, I need to create a custom command that will be used for inserting file path and file name of the current drawing (I should just click to define the place where I want to put it).

Link to post
Share on other sites
X11start

(getvar "dwgprefix")

 

ex.

"D:\MYDWG\SMITH\"

 

(getvar "dwgname")

 

ex.

"HOUSE_OF_MR_SMITH.DWG"

Edited by X11start
Link to post
Share on other sites
Tharwat
(defun c:Test (/ ins str)
  ;; Tharwat - Date: 29.Mar.2021	;;
  (and (or (> (getvar 'DWGTITLED) 0)
           (alert "Save the drawing then try again!")
       )
       (setq ins (getpoint "\nSpecify the insertion point : "))
       (setq str (entmakex (list '(0 . "MTEXT")
                                 '(100 . "AcDbEntity")
                                 '(100 . "AcDbMText")
                                 (cons 10 (trans ins 1 0))
                                 '(1 . "")
                           )
                 )
       )
       (vla-put-textstring
         (vlax-ename->vla-object str)
         "%<\\AcVar Filename \\f \"%tc4%fn7\">%"
       )
  )
  (princ)
)

 

  • Like 1
Link to post
Share on other sites
rkmcswain

@mark_kostic - you might also create a block that contains a FIELD that contains the drawing path and filename, then you can just insert this block each time, or include it in your templates.

  • Like 2
Link to post
Share on other sites
tombu

Field examples you can add to template title block since they reference the current document not drawing objects.

Drawing path and filename with file extension:

%<\AcVar Filename \f "%fn7">%

Drawing path and filename without file extension:

<\AcVar Filename \f "%fn3">%

This way they will automatically update if you move or rename the drawing.

  • Like 2
Link to post
Share on other sites
  • 5 weeks later...
mark_kostic

Thanks for your responses.
I ended up with this code:
 

(defun c:pdm (/ ins str)
  (and (or (> (getvar 'DWGTITLED) 0)
           (alert "Save the drawing then try again!")
       )
       (setq ins (getpoint "\nChoose where you want to insert the file name : "))
       (setq str (entmakex (list '(0 . "MTEXT")
                                 '(100 . "AcDbEntity")
                                 '(100 . "AcDbMText")
                                 (cons 10 (trans ins 1 0))
                                 '(1 . "")
				
                           )
                 )
       )
       (vla-put-textstring (vlax-ename->vla-object str)

			"{\\fArial|b1|i0|c0|p34;\\C2;%<\\AcVar Filename \\f \"%tc4%fn6\">%}"

)
  )
  (princ)
)

 

 

Is there a way to define the text size (height) in this code?

  • Dislike 1
Link to post
Share on other sites
Lee Mac

I would strongly suggest controlling the typeface through an appropriate Text Style, and the colour using either the layer colour (with object colour set to ByLayer) or using object colour, in place of using MText formatting overrides embedded within the text content.

 

I would personally opt to write the code in the following way:

(defun c:pdm ( / ins obj )
    (cond
        (   (zerop (getvar 'dwgtitled))
            (princ "\nThe current drawing is unsaved.")
        )
        (   (setq ins (getpoint "\nSpecify insertion point: "))
            (setq obj
                (vla-addmtext
                    (vlax-get-property
                        (vla-get-activedocument (vlax-get-acad-object))
                        (if (= 1 (getvar 'cvport)) 'paperspace 'modelspace)
                    )
                    (vlax-3D-point (trans ins 1 0))
                    0.0
                    "%<\\AcVar Filename \\f \"%tc4%fn6\">%}"
                )
            )
        )
    )
    (princ)
)
(vl-load-com) (princ)

 

Currently, the text height is controlled by the TEXTSIZE system variable, however, you can add an expression such as the following to override the height to something other than that defined by the Text Style:

(vla-put-height obj 2.5)

 

 

Edited by Lee Mac
Link to post
Share on other sites
Tharwat
31 minutes ago, mark_kostic said:

Thanks for your responses.
I ended up with this code:
 


(defun c:pdm (/ ins str)
  (and (or (> (getvar 'DWGTITLED) 0)
           (alert "Save the drawing then try again!")
       )
       (setq ins (getpoint "\nChoose where you want to insert the file name : "))
       (setq str (entmakex (list '(0 . "MTEXT")
                                 '(100 . "AcDbEntity")
                                 '(100 . "AcDbMText")
                                 (cons 10 (trans ins 1 0))
                                 '(1 . "")
				
                           )
                 )
       )
       (vla-put-textstring (vlax-ename->vla-object str)

			"{\\fArial|b1|i0|c0|p34;\\C2;%<\\AcVar Filename \\f \"%tc4%fn6\">%}"

)
  )
  (princ)
)

 

 

Is there a way to define the text size (height) in this code?

 

Its a bad idea to end up with removing authors' names from their original codes.

 

 

 

 

  • Like 1
Link to post
Share on other sites
tombu
4 hours ago, Lee Mac said:

I would strongly suggest controlling the typeface through an appropriate Text Style, and the colour using either the layer colour (with object colour set to ByLayer) or using object colour, in place of using MText formatting overrides embedded within the text content.

 

You may want to change the Text Style to Arial Narrow to squeeze a long file path in later. Why complicate this?

I rarely use Mtext formatting overrides other than in blocks using Standard Text Style so they display the same for everyone no matter what Text Styles they have in their drawings.

It's actually the only time I use the Standard Text Style. Never any confusion what an Arial Text Style will look like.

Link to post
Share on other sites
Lee Mac
7 hours ago, tombu said:

You may want to change the Text Style to Arial Narrow to squeeze a long file path in later. Why complicate this?

I rarely use Mtext formatting overrides other than in blocks using Standard Text Style so they display the same for everyone no matter what Text Styles they have in their drawings.

It's actually the only time I use the Standard Text Style. Never any confusion what an Arial Text Style will look like.

 

I couldn't quite work out whether you agreed or disagreed with me here 😆

Link to post
Share on other sites
tombu
12 hours ago, Lee Mac said:

 

I couldn't quite work out whether you agreed or disagreed with me here 😆

Sorry should have made that clearer. I agreed!😁

There are no absolutes though. For a fancy N for a North Arrow block without adding another Text Style to any drawing it's inserted in is one. 

Thanks again for all the code like Steal from Drawings I use every day. Being able to quickly add whatever size layout I need with title block and Plot Styles for both plotting and sending to PDF.

^C^C^P(Steal (strcat (vl-filename-directory (getenv "QnewTemplate")) (chr 92) "AutoCAD Template" (chr 92) "TemplateBlks.dwt") (list (list "Page Setups" (list "11×17" "11×17 PDF"))(list "Layouts" (list "11×17"))))(load "Glblattchg.lsp")(Glblattchg "11×17" "#P" "Layouts") .regen

With code from you and others whenever I add or move layouts around page numbers and number of pages automatically update. With a few clicks I select whoever's signature is required and fill in most of the attributes. Like to think I've contributed online as well but am forever grateful for all the help and code I've gotten from forums like this.

  • Like 1
Link to post
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
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...