Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Change to layer 0 on exit/close

10 REPLIES 10
SOLVED
Reply
Message 1 of 11
klugb
1194 Views, 10 Replies

Change to layer 0 on exit/close

I am using 2011 Civil 3D and there is a bug I am trying to get a workaround for. If your drawing is not closed with layer 0 being the current layer several different civil 3D entities assume the color of the current layer of xref'd drawings.

 

From my reading it appears I need a reactor to thaw layer 0 and make it current before close or exit commands. I am not a programmer and have struggled making this work.

 

Any guidance would be appreciated.

 

Bruce

Bruce Klug, P.E.
AutoCAD Expert Elite Alumni
AutoCAD Civil 3D Certified Professional
Civil 3D 2023.2.1

Win 10 Enterprise, 64-bit
10 REPLIES 10
Message 2 of 11
honkinberry
in reply to: klugb

Well I started on this, but now I'm not sure if it is possible.

 

This is what the reactor would look like:

 

(defun dwgshutdown (reactor info / layer0 changed)
	(setq layer0 (entget (tblobjname "LAYER" "0")))
	(if (not (zerop (cdr (assoc 70 layer0))))
		(setq layer0 (subst (cons 70 0) (assoc 70 layer0) layer0)
			layer0 (entmod layer0)
			changed t
		) ; setq
	) ; if
	(if (/= (getvar "CLAYER") "0")
		(setq changed (setvar "CLAYER" "0"))
	) ; if
) ; dwgshutdown

(vlr-remove-all :vlr-dwg-reactor)
(vlr-dwg-reactor nil (quote ((:vlr-beginClose . dwgshutdown))))

 

But I can't seem to save the changed file.
I added the changed flag, to have something a (if changed (command "_.QSAVE"))
but it seems that trying to save the drawing from within a dwgshutdown reactor is not right.
Perhaps someone with a little experience in this matter can pick up where I left off.
--J

 

Message 3 of 11
pbejse
in reply to: honkinberry

I would rather use :vlr-beginSave

After working on a file, the first thing Autocad will ask when you invoke _close/_quit is do you want to save?

If you're not goping to save,, why would you set the layer to 0 at all

 

something like this:

 

(setq set_0
(vlr-dwg-reactor nil '((:vlr-beginSave . save_Set_0))))


(defun save_Set_0 (Caller CmdSet / lyrs lyr_0)
(setq lyrs (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))))
(if (equal
	      :vlax-true
	      (vla-get-freeze (setq lyr_0 (vla-item lyrs "0"))))
	      (vla-put-freeze lyr_0 :vlax-true))
(setvar "Clayer" "0")
)

 

Think about it   Smiley Happy

Message 4 of 11
klugb
in reply to: pbejse

It appears now that it's a bigger problem then just setting the drawing back to layer 0 upon closing. It is whatever layer is current that a bunch of Civil 3D objects change color to the current layer.

Thanks for the help, but this is a bug with Autodesk and I have submitted a support request to them.

 

Bruce

Bruce Klug, P.E.
AutoCAD Expert Elite Alumni
AutoCAD Civil 3D Certified Professional
Civil 3D 2023.2.1

Win 10 Enterprise, 64-bit
Message 5 of 11
klugb
in reply to: pbejse

pbejse,

 

I am back working on this because we need this fixed to use the publishing options.

 

I got you code working but didn't want it changing layers with every save & qsave just on closing the drawing.

I modified you code and changed it to use "vlr-BeginClose" instead of "vlr-beginSave" thinking this would solve my problem, but it didn't work. Have you used that one?

 

Also, do you know how to thaw a layer while using reactors? I have to make sure layer 0 is frozen before using the Clayer setvar.

 

Thanks for the help,

 

Bruce Klug, P.E.
AutoCAD Expert Elite Alumni
AutoCAD Civil 3D Certified Professional
Civil 3D 2023.2.1

Win 10 Enterprise, 64-bit
Message 6 of 11
pbejse
in reply to: klugb

Reading post #1, Cant really say for sure but I think you're going about his the wrong way my friend.

 

I suggest you try a different approach.

I believe there are settings for layers on specific task in Civil 3D, maybe you should explore that.

 

as for the ractor vlr-BeginClose issue, as you can tell by honkinberry's post, changes made prior to closing the file will be ignored unless you save it first. (not sure though, thats why i opted for Save)

 

Could you kindly explain this one for us again,

"if your drawing is not closed with layer 0 being the current layer several different civil 3D entities assume the color of the current layer of xref'd drawings."

 

then

" thaw layer 0 and make it current before close or exit commands."

now

'"I have to make sure layer 0 is frozen before using the Clayer setvar."

 

I'm confused, which one do you want?

 

I'm not really a Civil 3D user, but from what i gathered from my other co-workers, layer colors should not be an issue if done correclly.

 

I'll investigate further and i'll get back to you whenever i found something useful, but for the meantime, check layer settings, I will try it on my end and myabe we can find a solution to this together.

 

Cheers

  

  

Message 7 of 11
klugb
in reply to: klugb

Thanks, but I got it working yesterday.

I was trying to say that if layer 0 is frozen I need a way to thaw it before using the setvar CLAYER.

The blocks and layer colors were done correctly, until we switched to Civil 3D 2011. There is a bug that has been confirmed by Autodesk support that they are working on. The only solution to the bug is to make sure that layer 0 is the current layer when you open the drawing. My solution was to make it current as you close the drawing.

 

Thanks for the help, I merged several different peoples codes to make it work.

Check it out: 

http://screencast.com/t/GcDT4eGvmo

 

This is what I am running now:

;;=======================================================================
;; CLOSE reactor
;;=======================================================================
(vl-load-com)
(defun smart-command-reactor (commands StartCallback EndCallback / ended)
  (vl-load-reactors)
  (setq ended
    (vlr-command-reactor nil
     '(
        (:vlr-commandEnded . internal-commandEnded)
        (:vlr-commandCancelled . internal-commandEnded)
        (:vlr-commandFailed . internal-commandEnded)
      )
    )
  )
  (vlr-remove ended)
  (vlr-command-reactor
    (list
      ended
      StartCallback
      EndCallback
      (if (listp commands)
        (mapcar 'strcase commands)
        (list (strcase commands))
      )
    )
    '((:vlr-commandWillStart . internal-commandWillStart))
  )
)
;;=======================================================================
(defun internal-commandWillStart (reactor args / data result)
  (setq data (vlr-data reactor))
  (if
    (and
      (member (car args) (last data))
      (setq result (apply (cadr data) (list (car args))))
    )
    (progn
      (vlr-data-set
        (car data)
        (list (caddr data) result)
      )
      (vlr-add (car data))
    )
  )
)
;;=======================================================================
(defun internal-commandEnded (reactor args / data)
  (setq data (vlr-data reactor))
  (vlr-remove reactor)
  (apply
    (car data)
    (list
      reactor
      (vlr-current-reaction-name)
      (car args)
      (cadr data)
    )
  )
)
;;=======================================================================
(if *my-smart-close-reactor* (vlr-remove *my-smart-close-reactor*))
;;=======================================================================
(setq *my-smart-close-reactor*

  (smart-command-reactor

    ;; command(s) as list
    '("CLOSE")

    ;; StartCallback (called when CLOSE starts)
    (function
      (lambda (cmdname)
       (setq lyrs (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))))
(if (equal
       :vlax-true
       (vla-get-freeze (setq lyr_0 (vla-item lyrs "0"))))
       (vla-put-freeze lyr_0 :vlax-false))
(setvar "Clayer" "0")
      )
    )

    ;; EndCallback (called when CLOSE ends)
    (function
      (lambda (data)
        (setvars data)
        (sssetfirst nil)
        (setq app nil adoc nil)
      )
    )
  )
)
;;=======================================================================
(defun setvars (data)
  (mapcar
    (function
      (lambda (v / r)
        (setq r (getvar (car v)))
        (setvar (car v) (cdr v))
        (cons (car v) r)
      )
    )
    data
  )
)
;;=======================================================================
(princ "\nCLOSE reactor enabled.")
;;=======================================================================
(princ)
;;=======================================================================

Bruce Klug, P.E.
AutoCAD Expert Elite Alumni
AutoCAD Civil 3D Certified Professional
Civil 3D 2023.2.1

Win 10 Enterprise, 64-bit
Message 8 of 11
klugb
in reply to: klugb

I guess it's still a work in progress after all. I just noticed the "error: too many arguments" issue. I also noticed that it does not work if I close Autocad with the "X" (not the drawing) which issues _Quit and not _close.

 

Bruce Klug, P.E.
AutoCAD Expert Elite Alumni
AutoCAD Civil 3D Certified Professional
Civil 3D 2023.2.1

Win 10 Enterprise, 64-bit
Message 9 of 11
pbejse
in reply to: klugb

Try this

 

(setq set_0
(vlr-dwg-reactor nil '((:vlr-beginclose . save_Set_0))))


(defun save_Set_0 (Caller CmdSet / lyrs lyr_0)
(setq lyrs (vla-get-layers (setq 
             adoc (vla-get-activedocument (vlax-get-acad-object)))))
(if (equal
	      :vlax-true
	      (vla-get-freeze (setq lyr_0 (vla-item lyrs "0"))))
	      (vla-put-freeze lyr_0 :vlax-false))
		(vla-setvariable  adoc "clayer" "0")
  		(vla-save (vla-get-activedocument (vlax-get-acad-object)))
)

 

 

In theory it should work

Message 10 of 11
troma
in reply to: pbejse

Did it work?

 

I'm having the same problem.  Currently I'm just trying to remember to make layer 0 current every time before I close.  I did a quick search on the forums for a way to automate this, and I found this thread.

 

I have no idea how to run the code, but before I get into that issue I'd like to know: did the final version posted here work or not?  Any problems in the real life application?

 

Thanks!


Mark Green

Working on Civil 3D in Canada

Message 11 of 11
klugb
in reply to: klugb

We ended up switching to 2012 and don't have the problem any longer. The code I attached worked fine, but reports "Too many arguments" as it's closing. I switched to 2012 before figuring it out.

 

 

Bruce Klug, P.E.
AutoCAD Expert Elite Alumni
AutoCAD Civil 3D Certified Professional
Civil 3D 2023.2.1

Win 10 Enterprise, 64-bit

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

”Boost