PDA

View Full Version : Find file format through LISP?


Mike_R
2007-10-05, 04:16 PM
Hey guys. I've just written a routine to save a file back to it's original format (R14, 2004, 2007...), but the way it's done it uses the "lastprompt" system variable and requires that "CMDECHO" be turned on. I hate doing that because the verbiage may very well change and I don't want to have to rewrite any part of it.

Is there a way to access this information through LISP or VLISP without pulling the info from a prompt?

Avatart
2007-10-05, 04:20 PM
Show the dog the rabbit, post that baby up and we'll look under the bonnet.

Mike_R
2007-10-05, 04:52 PM
Nothin' complicated really... Just need a way to access the current DWG version...

Here's the code for it as it stands now.


(defun c:qsave (/ xpert echo)
(setq xpert (getvar "EXPERT")
echo (getvar "CMDECHO")
)
(if (wcmatch (strcase (vla-get-name acaddocument)) "DRAWING*")
(progn
(initdia)
(vl-cmdf "_.saveas")
)
(progn
(setvar "CMDECHO" 1)
(setvar "EXPERT" 2)
(vl-cmdf "_.SAVEAS")
(setq promt (getvar "LASTPROMPT"))
(cond
((vl-string-search "R14" promt)(vl-cmdf "R14" "")(prompt "\nDrawing saved as R14"))
((vl-string-search "2000" promt)(vl-cmdf "2000" "")(prompt "\nDrawing saved as 2000"))
((vl-string-search "2004" promt)(vl-cmdf "2004" "")(prompt "\nDrawing saved as 2004"))
((vl-string-search "2007" promt)(vl-cmdf "2007" "")(prompt "\nDrawing saved as 2007"))
(nil exit)
)
(setvar "CMDECHO" echo)
(setvar "EXPERT" xpert)
)
)
(princ)
)

Avatart
2007-10-05, 04:59 PM
Why not use something like:

(getvar "ACADVER")

Mike_R
2007-10-05, 05:08 PM
acadver gets the current version of AutoCAD that you're running. What I want is the current format of the drawing. Most of our files are saved to 2000 format, so we have our default save format set to 2000. There are some things I prefer to save as 2007 (so as to not lose information), but qsave will only save it to the default format, so a saveas is required to keep it at 2007.

I've gotten tired of having to remember to saveas to the proper format if I want to keep my information, since I often forget and then sometimes lose imformation when I open the drawing again, so I wrote this little nugget. Only problem is, I can't find a good way of getting the current file format, so I had to resort to this bit of kludgery.

Opie
2007-10-05, 05:09 PM
That returns the current AutoCAD version and not the drawing version.

Try this. It returns a sting of the version.
(defun dwgver
(/ DWGFILEPOINTER DWGFORMAT DWGINDEX DWGNAME DWGVERSION)
(vl-load-com)
(setq DWGName (strcat (getvar "DWGPREFIX") (getvar "dwgname")))
(if (findfile DWGName)
(progn
(setq DWGFilePointer
(open DWGName "r")
DWGFormat ""
)
(repeat 5
(setq DWGFormat
(strcat DWGFormat (chr (read-char DWGFilePointer)))
)
)
(while (and (null (setq DWGIndex (vl-position
DWGFormat
'("MC0.0" "AC1.2"
"AC1.4" "AC1.50"
"AC2.10" "AC1002"
"AC1003" "AC1004"
"AC1006" "AC1009"
"AC1012" "AC1014"
"AC1015" "AC1018"
"AC1021"
)
)
)
)
(< (strlen DWGFormat) 10)
)
(setq DWGFormat
(strcat DWGFormat (chr (read-char DWGFilePointer)))
)
)
(close DWGFilePointer)
(if DWGIndex
(setq DWGVersion
(cadr (nth DWGIndex
(list
'("MC0.0" "Rel. 1.1") '("AC1.2" "Rel. 1.2")
'("AC1.4" "Rel. 1.4") '("AC1.50" "Rel. 2.0")
'("AC2.10" "Rel. 2.10") '("AC1002" "Rel. 2.5")
'("AC1003" "Rel. 2.6") '("AC1004" "Rel.9")
'("AC1006" "Rel.10") '("AC1009" "Rel.11/12")
'("AC1012" "Rel.13") '("AC1014" "Rel.14") '("AC1015" "AutoCAD 2000")
'("AC1018" "AutoCAD 2004") '("AC1021" "AutoCAD 2007"))
)
)
)
)
)
(alert "Current drawing is not found or saved.")
)
(if DWGVersion
DWGVersion
nil
)
)

To run
(dwgver)

Avatart
2007-10-05, 05:21 PM
Sorry, misread question. :Oops:

Mike_R
2007-10-05, 05:35 PM
Thanks again Opie!

And don't worry Avatart, we've all done it.

Cheers! :beer:

Opie
2007-10-05, 05:39 PM
Thanks again Opie!

And don't worry Avatart, we've all done it.

Cheers! :beer:
I see you have your username changed. ;)

Hope it is useful. Of course, as new formats are created, this routine would need to be updated to accomodate the new versions.

Avatart
2007-10-05, 05:46 PM
I see you have your username changed. ;)

Hope it is useful. Of course, as new formats are created, this routine would need to be updated to accomodate the new versions.
Who, me or Mike? :?

Is there a pattern to the version naming? I was wondering if it could be future proofed by working out what version names it would produce in the future.

Opie
2007-10-05, 05:51 PM
Who, me or Mike? :?

Is there a pattern to the version naming? I was wondering if it could be future proofed by working out what version names it would produce in the future.
I was talking about Mike's. I know your username has been changed, but that's been a few days. Mike had asked about it yesterday.

There probably is a pattern to the version numbers at the moment. However, I would hate to try to future proof the routine, and Autodesk decides to change the pattern. Then the routine would just be broken for newer versions.

The code does need a little massaging to take into account future versions. Right now, it would just return nil, I think. ;) It would just be broken with an endless loop.

Edit: The code above has been edited to reflect some of this future proofing. The original code would possibly hang or crash during the while loop if a format was not found. The new code now has a limit of 9 characters to test for the while loop. Of course, if the version at the beginning of the drawing file is longer than that, the code will need to be modified to accommodate.

Avatart
2007-10-05, 06:00 PM
and Autodesk decides to change the pattern.
Shirley not? :shock:

Mike_R
2007-10-05, 06:06 PM
Yes, this may need to be massaged for future versions, but my company wouldn't have to worry about that unless we upgrade to that version... But even then, I wouldn't have written it into it's own routine by myself if it were any simpler (I think I feel a wish coming on).

With this little routine, if I ever decide to do anything else that requires a particular dwg format, I'll still only have to change the 1 routine, rather than mulitple routines. Still better if you ask me.

Opie
2007-10-05, 06:06 PM
Shirley not? :shock:
It could happen and don't call me Shirley. ;)