Variables Not Being Set After a "SaveAs" and an "Open"

Variables Not Being Set After a "SaveAs" and an "Open"

kpennell
Collaborator Collaborator
1,228 Views
14 Replies
Message 1 of 15

Variables Not Being Set After a "SaveAs" and an "Open"

kpennell
Collaborator
Collaborator

In an effort to control the thousands of documents we work on for clients, I'm trying to introduce some limitations in drawings that we have issued to clients. The ideal workflow is that, when a drawing gets issued, it is set as read-only by the lead, or document control personnel (details to be ironed out later).

 

So I came up with some code to take the read-only drawing name and revision, do a saveas, and an open (I need to do an 'open' here, because a third party add-on still see the drawing as a 'read-only' drawing, even though the 'WriteStat' variable is set at 1).

 

 (setvar "filedia" 0)
 (setq FileBase (vl-filename-base (getvar "dwgname")))
 (setq SetReadOnly (strcat (getvar "dwgprefix") (getvar "dwgname")))
 (setq _Position (vl-string-search "_" FileBase))
 (setq CurrentRevision (substr FileBase (+ _Position 3)))
 (setq FileBaseR (substr FileBase 1 (+ _Position 2)))
 (setq NewRevisionString (itoa (+ 1 (atoi CurrentRevision))))
 (setq NewDwgName (strcat FileBaseR NewRevisionString))
 (setq PathDwgName (strcat (getvar "dwgprefix") NewDwgName ".dwg"))
 (if (findfile PathDwgName)
  (prong

   (Alert (strcat "\"" NewDwgName "\" already exists!"))
   (exit)
  );progn
 )
 (command "_.saveas" "" PathDwgName)
 (command "_.open" PathDwgName)

 

After this though, none of my 'setq' functions are working. Though everyone one of my 'command' and 'setvar' functions are working.

 

If I comment out the (command "_.open" PathDwgName) using ";" the setq functions work perfectly.

 

I wouldn't think I'd need to use 'vl-bb-set' and 'vl-bb-ref' here because as I stated above:

 

(command "-layer" "set" "0" ""); works

(setq AssemblyListEnts (ssget "_X" '((0 . "ACAD_TABLE") (1 . "ASSEMBLIES")))); does not work

(command "zoom" "extents"); works

 

all the 'setq' work is done in the one drawing.

0 Likes
1,229 Views
14 Replies
Replies (14)
Message 2 of 15

dbroad
Mentor
Mentor

Without getting into the details of what you are trying to do, are you expecting the saved AutoLISP variables to be persistent between sessions?  If so, you need to find another way to work because each time you close a drawing or reopen a drawing the AutoLISP system is flushed and refreshed.

Architect, Registered NC, VA, SC, & GA.
0 Likes
Message 3 of 15

Ranjit_Singh
Advisor
Advisor

When you use setq the variable is only valid in that document and it's scope is only limited to that session (unless you end it before the session ends by setting it to nil). Re-opening the drawing will not give you access to any of your setq variables. You either need to create a dictionary object so it can always stay with the drawing or use USERR(S, I) variables which stay with the drawing. vl-bb-set and ref are not tied to the drawing but to the acad session.

0 Likes
Message 4 of 15

kpennell
Collaborator
Collaborator

If I can just add this note for clarification:

 

I'm not doing anything from drawing to drawing, or from session to session.

 

So basically and rudimentary:

 

1. Do a saveas. I haven't done any 'setq' functions prior to this.

2. Do an open, because the 'add-on' software still sees the drawing as read-only, even though the writestat variable is at 1.

3. Perform a layer command, which works, which means the code is following through.

4. Getting a bunch of data from entities using the 'setq' functions, but not working.

4. Performing a 'zoom' 'extents' command which also works, and means the code is following through.

 

If it is the way it is, so be it, and I'll modify the ideal workflow.

 

Thanks all your input on this.

0 Likes
Message 5 of 15

dbroad
Mentor
Mentor

Your sequence as listed in your last post contradicts your code as listed in the OP.  In your code, the setq statements come before the open command.  Once the open command is performed, all preceding LISP symbols are reset.  I don't know what AutoCAD version you are using but (command "open"  ....) won't work unless SDI is set to 1.  If you want to open a document in a multidocument mode, then it's best to use activeX.

@Ranjit_Singh mentioned the blackboard. In general, I have had very mixed results when trying to funnel information from one drawing to another with the blackboard.  If you need information about the same drawing between sessions (before and after the open command), then using a dictionary or an external text file will work.

Architect, Registered NC, VA, SC, & GA.
0 Likes
Message 6 of 15

kpennell
Collaborator
Collaborator

Right.

 

I didn't mean to say that I'm not performing any 'setq' function prior to the saveas and open.  What is true, is that I'm not trying to call any of 'setq' functions prior to the saveas and open to after. Any new 'setq' functions after the save are just not setting. And it just doesn't make sense to me that 'command' functions are working fine, but not 'setq' functions after the saveas and open.

 

Like I said, if there is that valid reason out there, then I can accept that.

 

Hope that makes sense, and thanks again for your time.

0 Likes
Message 7 of 15

Anonymous
Not applicable

can you try this instead of the open command?

 

(vla-open (vla-get-documents (vlax-get-acad-object)) PathDwgName)
Message 8 of 15

kpennell
Collaborator
Collaborator

I just tested this, and...

 

; error: Automation Error. Method not available in SDI mode.

 

And I don't have a whole lot of confidence the AutoCAD add-on i mentioned earlier will support MDI mode.

 

Really appreciate the suggestion.

0 Likes
Message 9 of 15

Anonymous
Not applicable

Outside the box ridiculous suggestion. Write your lisp and right after the saveas have it write a script to the same folder that it saved the dwg to. Write the script to open the drawing and run a second lisp, which would have the rest of your planned routine, and would also delete the script.

 

Seems overly complicated, but it's pretty simple really.

Message 10 of 15

dbroad
Mentor
Mentor

Thanks for confirming you are in SDI=1 mode.  This means that there will be no blackboard and attempts to access other documents will fail.

 

I don't have time to write programs for you but you might want to do internet searches for 

autolisp save variables to dictionary.

 

A link that I found actually suggests using Ldata rather than dictionaries.  You might try that approach.

 

http://forums.augi.com/archive/index.php/t-158466.html

Architect, Registered NC, VA, SC, & GA.
0 Likes
Message 11 of 15

kpennell
Collaborator
Collaborator

It's funny, so what I've done is this for now: When I do this:

 

(command "_.saveas" "" PathDwgName)

(command "_.open" PathDwgName)

(Alert "Type \"UA\" again to follow through with the updating process!")(exit)

 

The 'UA' command is the command I'm trying to develop.

 

Even if I do this:

 

(command "_.saveas" "" PathDwgName)

(command "_.open" PathDwgName)

(load "W:\\Lisp\\Assemblies\\Production-UA.vlx")

(C:UA)

 

is still does not fulfill the 'setq' functions. It quite boggles me.

0 Likes
Message 12 of 15

Ranjit_Singh
Advisor
Advisor

@Anonymous wrote:

...........................

A link that I found actually suggests using Ldata rather than dictionaries...............


Ldata in itself is not an object/entity. It just is a means of writing/reading Lisp data to/from a dictionary (or an entity or object). In OP's case it does not seem like he is trying to attach any data to entities so he will, most likely, end up creating a dictionary object in the drawing's named object dictionary. He could do it by ActiveX methods or plain LISP.

0 Likes
Message 13 of 15

Anonymous
Not applicable

Use this. works like a charm, I tested it. have it in your code, and instead of the command call just use (LM:Open PathDwgName)

 

;;------------------------=={ Open }==------------------------;;
;;                                                            ;;
;;  Uses the 'Open' method of the Shell Object to open the    ;;
;;  specified file or folder.                                 ;;
;;------------------------------------------------------------;;
;;  Author: Lee Mac, Copyright © 2011 - www.lee-mac.com       ;;
;;------------------------------------------------------------;;
;;  Arguments:                                                ;;
;;  target - file, folder or ShellSpecialFolderConstants enum ;;
;;------------------------------------------------------------;;
;;  Returns:  T if successful, else nil                       ;;
;;------------------------------------------------------------;;

(defun LM:Open ( target / shell result )
    (if
        (and
            (or
                (eq 'INT (type target))
                (setq target (findfile target))
            )
            (setq shell (vla-getInterfaceObject (vlax-get-acad-object) "Shell.Application"))
        )
        (progn
            (setq result (vl-catch-all-apply 'vlax-invoke (list shell 'open target)))
            (vlax-release-object shell)
            (not (vl-catch-all-error-p result))
        )
    )
)
Message 14 of 15

kpennell
Collaborator
Collaborator

so you're right, the 'setq' functions did work, but unfortunately, the add-on software still thinks it's a read-only drawing, even though the 'writestat' variable is at '1'.

 

I'm certainly going to post a ticket to the developer's ticketing system.

 

Thanks all, for your help.

0 Likes
Message 15 of 15

Anonymous
Not applicable

Goodluck!


@kpennell wrote:

so you're right, the 'setq' functions did work, but unfortunately, the add-on software still thinks it's a read-only drawing, even though the 'writestat' variable is at '1'.

 

I'm certainly going to post a ticket to the developer's ticketing system.

 

Thanks all, for your help.


 

0 Likes