View Full Version : Viewport layer Lisp program makes AutoCAD crash
bcowper
2005-01-27, 06:21 PM
I have recently upgraded to AutoCAD 2005 and I have been experiencing plotting errors, causing AutoCAD to crash. I have managed to track down a possible problem with my lisp routine that changes all viewports in a drawing to a standard layer that is non-printable. If i run my program on a new drawing with a single viewport in a layout and then audit the drawing I get the following message/fix:
Command: audit
Fix any errors detected? [Yes/No] <N>: y
1 Blocks audited
Auditing Entities Pass 1
DXF Name Current Value Validation Default
AcDbViewport(E8 )
Paperspace vport layer Not "0" "0"
Pass 1 2 objects audited
Pass 2 2 objects audited
Total errors found 1 fixed 1
Here's the lisp code :
; em-VPNPrint.lsp
;;;Moves all Viewports to non-printable layer 'g-anno-vprt'
;;;
;;;
(defun c:em-VPNPrint (/ doc
;;current document
lyr
;;layer to change to
blk
;;current block to evaluate
blkCol
;;block collection
obj
;;current object to evaluate
)
(setq doc (vla-get-activeDocument (vlax-get-acad-object))
;;gets the active drawing
blkCol (vla-get-blocks doc)
;;gets the block collection
lyr "g-anno-vprt"
;;standard viewport layer
clyr (getvar "clayer")
;;current layer
)
(command "-layer" "m" lyr "p" "n" "" "s" clyr "")
(vlax-for blk blkCol
;;check each block (paper space and model space are treated as blocks)
(vlax-for obj blk
;;check each object in the block
(if (= (vla-get-objectName obj) "AcDbViewport")
;;if object is a viewport
(vla-put-layer obj lyr)
;;put in on the layer
)
)
)
(print
"All Viewports have been placed on non-printable layer 'g-anno-vprt'"
)
(princ)
)
Any help would be appreciated.
kennet.sjoberg
2005-01-27, 09:07 PM
What about one line of old fashion code that will do the same thing ?
(command "change" (ssget "_X" '((0 . "VIEWPORT" ))) "" "p" "la" "g-anno-vprt" "" )
a viewport is not a single object, it is one viewport object and one polyline object glued together with a reactor.
: ) Happy Computing !
kennet
RobertB
2005-01-28, 01:49 AM
The issue is that your code is changing the invisible viewport that is always the first viewport in any layout. Your code should be written to skip that first viewport; we should not be changing it in any fashion.
mathew.worland
2005-01-28, 03:03 PM
Mr. Bell,
Can you expand on this invisible viewport?
Thank you,
Matt Worland
bcowper
2005-01-28, 03:14 PM
I was wondering about this mysterious invisible viewport too!
Also, would Kennet's old school code solve my problem?, I haven't had the time to work it into my code yet.
kennet.sjoberg
2005-01-29, 09:59 AM
The "old fashion code" is safe, it do not change the "invisible viewport"
: ) Happy Computing !
kennet
(defun c:em-VPNPrint ( / )
(if (not (tblsearch "LAYER" "g-anno-vprt" ) ) ;; if layer not exist, create it
(command "._-layer" "New" "g-anno-vprt" "Plot" "No" "g-anno-vprt" "" )
( )
) ;; send all ( almost ) viewports to viewport layer
(command "._change" (ssget "_X" '((0 . "VIEWPORT" ))) "" "p" "la" "g-anno-vprt" "" )
(print "All Viewports have been placed on non-printable layer 'g-anno-vprt' " )
(princ)
)
RobertB
2005-01-31, 05:19 PM
Can you expand on this invisible viewport?
Every layout, when it is created, gets a viewport assigned to it by AutoCAD. This automatic viewport isn't a floating viewport that you can see, but it does exist in the drawing's database. I don't know why it is created, other than perhaps that it provides support for multiple paperspaces. This invisible viewport is accessible via code thru the object model and entity data, but it does not pay to mess with it.
mathew.worland
2005-01-31, 05:38 PM
Thank you very much. I will add that into my code.
bcowper
2005-01-31, 07:08 PM
Thanks Kennet for the full code, works perfectly.
This has taught me that whenever I get back to lisping I'll make sure I audit my test drawings to see if my programs cause any similar errors before I start using them for real.
kennet.sjoberg
2005-02-01, 12:22 AM
pay, play or pray. . . Please Robert, show me how to entmod a viewport, I am not good enough.
; first create a mview
(setq VpDxf (entget (entlast)) )
(setq VpDxf (subst (cons 8 "K382" ) (assoc 8 VpDxf ) VpDxf ) ) ; change layer "0" to "K382"
(entmod VpDxf ) ; ==> nil
: ) Happy Computing !
kennet
. . by the way, I have a great feeling that the invisible viewport storing data when AutoCAD 2000+ save to release R14-, and then reuse that data to restore Layouts and other things from the block *Paper_Space and *Model_Space from R14-
. . but that is only a feeling.
stig.madsen
2005-02-01, 12:13 PM
show me how to entmod a viewport, I am not good enough.
Here's a consolation: No one is :)
You can't use ENTMOD on viewports, Kennet. Check out the help reference for ENTMOD for other restrictions to the function.
The task you describe can easily be done by using VLA-PUT-LAYER on the viewport as an object.
kennet.sjoberg
2005-02-01, 12:28 PM
Yes Stig, I know that I can not entmod a viewport,
I was fishing in Roberts last replay.
Thank You anyway. . .
vla-put-layer there you can modify the invisible viewport ( unsafe )
or use (command "._change" . . . as in a earlier replay ( safe )
: ) Happy Computing !
kennet
Thanks Kennet for the full code, works perfectly.
This has taught me that whenever I get back to lisping I'll make sure I audit my test drawings to see if my programs cause any similar errors before I start using them for real.
Here's how you'd use your original code, just omitting the PS viewport from the loop:
(vlax-for blk blkCol
;;check each block (paper space and model space are treated as blocks)
(setq idx 0);;set index back to 0
(vlax-for obj blk
;;check each object in the block after the first object which is the LO's VP
(if (= idx 0)
(setq idx (1+ idx));;skip the first object
(if (= (vla-get-objectName obj) "AcDbViewport")
;;if object is a viewport
(vla-put-layer obj lyr)
;;put in on the layer
)
)
)
)
This is the method I prefer (vs. "command"), as it is usable on drawings that are not the active drawing. ie-ObjectDBX, or in MDI mode on drawings in the background
kennet.sjoberg
2005-02-03, 12:02 AM
Yes miff, I agree,
This is the method I prefer (vs. "command"), as it is usable on drawings that are not the active drawing. ie-ObjectDBX, or in MDI mode on drawings in the background
but as I mention earlier with ( unsafe ) and ( safe ), powerful commands is like fire,
very good when _you know how to use, but very dangerous when _you do not !
: ) Happy Computing !
kennet
_you is not You miff ; )
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.