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

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

djharo71
Explorer Explorer
2,908 Views
29 Replies
Message 1 of 30

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

djharo71
Explorer
Explorer

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

0 Likes
2,909 Views
29 Replies
Replies (29)
Message 21 of 30

rolisonfelipe
Collaborator
Collaborator

 

What a lisp would be like replacing a point in rectangle and rotating in a line and an arc 
(setq ss (ssget "X" '((0 . "POINT"))) ct 0 ) (repeat (sslength ss) (setq inspt (cdr (assoc 10 (entget (ssname ss ct))))) (command "_circle" inspt 0.5) ; (setq ct (1+ ct)))
THIS IS MY BEST
 
(setq disX (getreal(getreal "\n SET X)))
(setq disY (getreal(getreal "\n SET Y)))
 
 
(setq SS (ssget "X" '((0 . "POINT"))) ct 0) ; select point
(repeat (sslength SS)
(setq inspt (cdr (assoc 10 (entget (ssname SS ct))))) 
(command "_.rectang" inspt SS _D  disX disY  ) associate rectangle wich poliline
(setq ct (1+ ct)))
 

 

0 Likes
Message 22 of 30

ec-cad
Collaborator
Collaborator

This lisp can find the 'SHEETS' block, and reports the Value.

You may want to blend in parts of this one, to 'find' those 'SHEETS' blocks.

;; Find_Sheets.lsp
;; To select all Blocks, look for 'SHEETS' as an Effective name.
;; Return just that block.

 (vl-load-com)
(defun C:GO (); remove if desired

 (defun get_anon_block ( blkname curtab )
  (vl-load-com)
  (setq BLKS (ssadd)); Nil Selection Set
  (setq UBLOCKS (ssget "X" (list (cons 0 "INSERT")(cons 2 "`*U*"))))
  (if UBLOCKS
   (progn
    (foreach ITEM (mapcar 'cadr (ssnamex UBLOCKS))
     (setq vl-block (vlax-ename->vla-object ITEM))
     (setq UBLKNAME (vla-get-effectivename vl-block))
     (if (= (strcase UBLKNAME) (strcase blkname))
      (setq BLKS (ssadd ITEM BLKS))
     ); if
    ); foreach
   ); progn
  ); if
   BLKS
 ); function

;; Call above function, searching for all 'SHEETS' blocks.
 (setq blk nil SHEET_NUMBER "")
 (setq tab (getvar "ctab"))
 (get_anon_block "SHEETS" tab)
;; show results
 (if blks
  (progn
   (setq N 0)
   (repeat (sslength blks)
    (setq elist (entget (ssname blks N)))
    (setq blkx (vlax-ename->vla-object (ssname blks N)))
     (if (safearray-value (setq atts (vlax-variant-value (vla-getattributes blkx))))
      (progn
       (setq atts (vlax-safearray->list (vlax-variant-value (vla-getattributes blkx))))
        (foreach att atts
         (setq Tag (vla-get-tagstring att))
         (if (= Tag "SHEET_NUMBER");         Sheet Number
          (setq SHEET_NUMBER (vla-get-textstring att))
         ); if
        ); foreach
       ); progn
     ); if
;; Check Sheet_Number
    (if (= SHEET_NUMBER tab); Checks SHEET_NUMBER to be same as Current Tab
     (progn
      (princ "\n")
       (if (/= SHEET_NUMBER "")
        (princ (strcat "\nFound Sheets Block This Layout with Sheet Number = " SHEET_NUMBER))
       ); if
      ); progn
    ); if
;; Report all Sheet_Numbers found
      (princ "\n")
       (if (/= SHEET_NUMBER "")
        (princ (strcat "\nFound Sheets Block with Sheet Number = " SHEET_NUMBER))
       ); if
   (setq N (+ N 1))
  ); repeat
  ); progn
 ); if
 (princ)
); function C:GO
(princ "\n")
(princ "\nType GO to run this program:")
(princ)

 

ECCAD

 

0 Likes
Message 23 of 30

rolisonfelipe
Collaborator
Collaborator

That wouldn't be right.

in fact, it would create predefined rectangles in the polyline

0 Likes
Message 24 of 30

ec-cad
Collaborator
Collaborator

What ?

You must have responded to the wrong message.

There are no 'rectangles' created with the code I supplied.!!

ECCAD

0 Likes
Message 25 of 30

Sea-Haven
Mentor
Mentor

Maybe his talking about post 14.

Message 26 of 30

rolisonfelipe
Collaborator
Collaborator

EXATAMENTE, EU ACHEI O PRODUTO FINAL TÃO FUNCIONAL, QUE EU CRIEI UMA VERSÃO DELE, EU TINHA COISAS, MAS COM A AJUDA DA OSNAP, FICOU PERFEITO!
EM SEGUIDA, IMPLEMENTEI AS FOLHAS E CALIBREI A SOBREPOSIÇÃO A DISTÂNCIA DE ONDE DEVERIA COMEÇAR E TERMINAR.

NESTA VERSÃO, É POSSÍVEL NOTAR QUE NÃO HÁ CONTROLE DE ONDE A ESTRADA COMEÇARÁ NA FOLHA

 

 

 

https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/overlapped-coordinates-fail/td-p/128...

 

rolisonfelipe_0-1717463301525.png

 

0 Likes
Message 27 of 30

Sea-Haven
Mentor
Mentor

Oops was meant for Rolinsonfelipe

 

Please english as well, yes in what I did asks for start end when selecting path, it also goes backwards a bit so 1st sheet does not start exactly on end of line/pline. I have hard coded the dx but a good idea if end user wants to change the overlap. Hidden in code is size factors depending on sheet size and scale. See image post 14.

 

0 Likes
Message 28 of 30

rolisonfelipe
Collaborator
Collaborator

This petition aims to select inclined project areas.

This routine has absurd potential, in the case of selection it is possible to maintain the rectangular coordinate selection, and also insert the selection with polar coordinate, type, starting point, base limit to define the angle, and then with the 3 click define the limit of the area?

If this is possible, when inserting the view port in the layout, the previously selected limit is rotated with alingspace.

Congratulations in advance on your masterpiece

 

 

rolisonfelipe_0-1717523058313.png

 

0 Likes
Message 29 of 30

baksconstructor
Advocate
Advocate

Have you tried this - AutoViewport    ?

0 Likes
Message 30 of 30

rolisonfelipe
Collaborator
Collaborator

This lisp here answers flatly for linear works, I have another LSP that creates Viewport, but it just has to adjust to polar coordinate, it is still in rectangular coordinate

0 Likes