Current drawing's path and filename in autolisp

Current drawing's path and filename in autolisp

Anonymous
Not applicable
8,474 Views
20 Replies
Message 1 of 21

Current drawing's path and filename in autolisp

Anonymous
Not applicable

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

0 Likes
Accepted solutions (2)
8,475 Views
20 Replies
Replies (20)
Message 2 of 21

ВeekeeCZ
Consultant
Consultant

Check these system variables

DWGNAME

DWGPREFIX

0 Likes
Message 3 of 21

hak_vz
Advisor
Advisor
(defun c:fileloc ( / pt th)
(setq pt (getpoint "\nSelect placement point >"))
(setq th  (getvar 'textsize))
(setvar 'cmdecho 0)
(command "_.text" pt th 0 (strcat (getvar 'dwgprefix)(getvar 'dwgname)) "") 
(setvar 'cmdecho 1)
(princ)
)

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 4 of 21

Anonymous
Not applicable

Thanks. This returns the file path, but the problem is when I save the drawing in a different folder. It doesn't automatically change the file location nor the file name in the drawing.
I need to insert multiline text (mtext) -> Insert Field... -> Field category: Document -> Field names: Filename -> Format: (none) -> Path and filename. This will automatically change the file path and the file name. I just do not know how to implement this in autolisp.

0 Likes
Message 5 of 21

hak_vz
Advisor
Advisor

@Anonymous wrote:

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


In your original request you didn't mention how you want this data to be inserted i.e MText with fields.

Check this link for quick field generation

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 6 of 21

_Tharwat
Advisor
Advisor
Accepted solution
(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)
)
Message 7 of 21

Kent1Cooper
Consultant
Consultant

Don't bother with AutoLisp or Mtext or a Field.  Use RTEXT.  It has the capability to use Diesel expressions, which can get at the file path and drawing name -- in fact, exactly that combination is one of the examples in >Help for it<.

 

If you use SAVEAS to save a drawing in a different folder [and/or to a different name], the Rtext content will initially look the same, but will update with a REGEN, or if you close the drawing, will update when you next open it.  If you put it in a different folder and/or name by other means [SAVE or WBLOCK command, file copying outside AutoCAD, etc.], it will be correct when you open such a new drawing.

Kent Cooper, AIA
0 Likes
Message 8 of 21

_Tharwat
Advisor
Advisor

I strongly recommend you to use AutoLISP rather than going with the old fashioned Rtext objects.

0 Likes
Message 9 of 21

Kent1Cooper
Consultant
Consultant

@_Tharwat wrote:

I strongly recommend you to use AutoLISP rather than going with the old fashioned Rtext objects.


Why?  Justify that.  Just because RTEXT has been around a long time [if that is, in fact, your point], or just because most people are not very familiar with it, doesn't make it something like "obsolete."  If "old-fashioned" is a reason not to use something, then you'd better not use LINEs either.

Kent Cooper, AIA
0 Likes
Message 10 of 21

_Tharwat
Advisor
Advisor

@Kent1Cooper wrote:

then you'd better not use LINEs either.

Seriously?

It's not your business if I use or not.

I forwarded my reply to the OP and they asked for AutoLISP command and not to you! and by the way, this forum is for Lisping FYI if were lost or so.

Got it or should I explain further?

0 Likes
Message 11 of 21

Kent1Cooper
Consultant
Consultant

@_Tharwat wrote:

... and by the way, this forum is for Lisping FYI ...


Seriously?  It's also for [see the title of the Forum] "General Customization," including such things as custom linetypes, hatch patterns, macros, scripts, Diesel, and various things other than just AutoLisp routines.

 

I still await a valid justification, other than just one's feeling about it or personal preference, for recommending against the use of RTEXT.

Kent Cooper, AIA
0 Likes
Message 12 of 21

ronjonp
Mentor
Mentor

Doesn't RTEXT need express tools to be loaded for it to work?

0 Likes
Message 13 of 21

Kent1Cooper
Consultant
Consultant

@ronjonp wrote:

Doesn't RTEXT need express tools to be loaded for it to work?


Yes, it is an Express Tool.  I don't see that as a reason not to use it -- after all, custom AutoLisp routines also need to be loaded to be used, but no one would suggest you shouldn't use them for that reason, any more than one would suggest you shouldn't use Lines because they're even more "old-fashioned" than RTEXT.

 

In addition, if you don't have them loaded, type EXPRESSTOOLS once in any drawing and they're loaded in every drawing forever.  It's not like AutoLisp routines that need to be loaded in every drawing every time it's opened [though at least that can be automated].

Kent Cooper, AIA
0 Likes
Message 14 of 21

ronjonp
Mentor
Mentor

 @Kent1Cooper I don't use lines because polylines are so much cooler. 😃

 

Kidding aside this can be accomplished either way .. IMO most people don't know diesel language and fields are pretty user friendly.

0 Likes
Message 15 of 21

Kent1Cooper
Consultant
Consultant

@ronjonp wrote:

.... I don't use lines because polylines are so much cooler. 😃

.... IMO most people don't know diesel language and fields are pretty user friendly.


[Just so you know, as cool as it may be, a single-segment Polyline equivalent to a Line or Arc uses more memory.  Only when you get a certain quantity of segments together does the Polyline that contains them begin to use less memory than the equivalent separate Line(s) and Arc(s).  But there are sometimes reasons to use them anyway, even in single segments, such as to be able to give them width.]

 

In this particular case, you don't need to know anything about Diesel at all -- you can take the appropriate Diesel expression directly from Help about RTEXT, because exactly what they're looking for is one of the examples.

Kent Cooper, AIA
0 Likes
Message 16 of 21

ronjonp
Mentor
Mentor

@Kent1Cooper wrote:

@ronjonp wrote:

.... I don't use lines because polylines are so much cooler. 😃

.... IMO most people don't know diesel language and fields are pretty user friendly.


....

 

In this particular case, you don't need to know anything about Diesel at all -- you can take the appropriate Diesel expression directly from Help about RTEXT, because exactly what they're looking for is one of the examples.


For sure but then you have to lead the horse to water. If you use a field, it's all right there in CAD. Does not get much easier than that IMO.

ronjonp_0-1617054506604.png

 

0 Likes
Message 17 of 21

Anonymous
Not applicable

Thanks a lot!! Exactly what I needed. Is it possible to put different font and color of the text within the multiline text in autolisp (I would like to put Arial bold and black color of the text)? Also, I need to insert the text "File path:" at the beginning of the field.
I am sorry if I am bothering with this, but I just started learning AutoLISP, so any help will be much appreciated. Thank you in advance!

0 Likes
Message 18 of 21

Anonymous
Not applicable

Thanks for your suggestion. I asked for autolisp solution because autolisp seemed (to me) somehow appropriate to solve the aforementioned problem. But I will definitely explore this too.

0 Likes
Message 19 of 21

_Tharwat
Advisor
Advisor
Accepted solution

Yes it is very possible with AutoLISP, so just replace the following part of the code with the above posted codes and please note is that the colour 7 is related to Black if your AutoCAD background is White and visa versa.

 

(vla-put-textstring (vlax-ename->vla-object str)

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

)

Message 20 of 21

Anonymous
Not applicable

It works for the color and the font!!! I am really thankful for your help!
Just, where should I add text to have it on the beginning of mtext field (I want to have text "File path:" as a prefix)?

Thank you again in advance!

0 Likes