i am using AI to write a script for setting viewports and it is telling me that It cant find my block labeled sheets any help would be greatly appreciated
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Here is the code it gave me and the error message.
(defun c:ZoomViewportsToSheets (/ doc layouts layoutName layout modelSpace blockName insPt vp sheetBlock sheetNumber)
(vl-load-com)
;; Document and Layouts
(setq doc (vla-get-activedocument (vlax-get-acad-object)))
(setq layouts (vla-get-Layouts doc))
(setq modelSpace (vla-get-ModelSpace doc))
;; Block names and attribute
(setq sheetBlockName "SHEETS"
attrName "SHEET_NUMBER")
;; Utility function to find a block by its attribute value in model space
(defun get-block-by-attribute (blockName attrValue / ss blk block found)
(setq ss (ssget "_X" (list (cons 0 "INSERT") (cons 2 blockName) (cons 410 "Model"))))
(if (and ss (> (sslength ss) 0))
(progn
(setq blkList (ssnamex ss))
(foreach blk blkList
(setq block (vlax-ename->vla-object blk))
(setq found (get-attribute block attrName))
(princ (strcat "\nChecking block: " (vlax-get block 'EffectiveName) " with attribute value: " found))
(if (and found (eq (strcase found) (strcase attrValue)))
(return block)))))
nil)
;; Utility function to get an attribute value from a block by its tag
(defun get-attribute (block attrName / attributes attribute tag)
(setq attributes (vla-getattributes block))
(vlax-for att attributes
(setq tag (vla-get-tagstring att))
(if (eq (strcase tag) (strcase attrName))
(return (vla-get-TextString att))))
nil)
;; Main function to process each layout
(defun process-layout (layout)
(setq layoutName (vla-get-Name layout))
(if (/= layoutName "Model")
(progn
(princ (strcat "\nProcessing layout: " layoutName))
(setvar "CTAB" layoutName)
;; Get the SHEETS block in model space by layout number
(setq sheetBlock (get-block-by-attribute sheetBlockName layoutName))
(if sheetBlock
(progn
(princ (strcat "\nFound SHEETS block for layout: " layoutName))
;; Get the insertion point of the SHEETS block
(setq insPt (vlax-get sheetBlock 'InsertionPoint))
;; Find and configure the viewport in paper space
(vlax-for obj (vla-get-PaperSpace doc)
(if (and (eq (vla-get-ObjectName obj) "AcDbViewport") (not (vla-get-ModelType obj)))
(progn
(setq vp obj)
(princ (strcat "\nConfiguring viewport for layout: " layoutName))
(vla-put-DisplayLocked vp :vlax-false)
(vla-put-Center vp (vlax-3d-point (car insPt) (cadr insPt)))
;; Optionally adjust viewport size as needed, e.g., set width and height to fit block extents
(vla-put-Height vp 200.0) ;; Adjust as necessary
(vla-put-Width vp 200.0) ;; Adjust as necessary
(vla-put-DisplayLocked vp :vlax-true)
)
)
)
)
(princ (strcat "\nSHEETS block not found for layout: " layoutName)))
)
)
)
;; Iterate through each layout and process
(vlax-for layout layouts
(process-layout layout))
(princ "\nZoom Viewports to SHEETS completed.")
(princ)
)
(princ "\nRun the function 'c:ZoomViewportsToSheets' to execute the script.\n")
(princ)
then when i run it in autocad map 3d
Command: ZOOMVIEWPORTSTOSHEETS
Processing layout: 03
SHEETS block not found for layout: 03
Processing layout: 04Regenerating layout.
Regenerating model.
SHEETS block not found for layout: 04
Processing layout: 05Regenerating layout.
Regenerating model.
SHEETS block not found for layout: 05
Processing layout: 06Regenerating layout.
Regenerating model.
SHEETS block not found for layout: 06
Processing layout: 07Regenerating layout.
Regenerating model.
SHEETS block not found for layout: 07
Processing layout: 08Regenerating layout.
Regenerating model.
SHEETS block not found for layout: 08
Zoom Viewports to SHEETS completed.
It basically runs through the sheets but does nothing because it claims to not see the blocks