Author Topic: Erase Outside Title Block  (Read 4621 times)

0 Members and 1 Guest are viewing this topic.

cmwade77

  • Swamp Rat
  • Posts: 1443
Erase Outside Title Block
« on: July 21, 2021, 06:09:21 PM »
I have code that works in AutoCAD, but because there is some ActiveX, it does not work in AcCoreConsole. Basically, this looks for a title block and erases anything outside of it. Can anyone help me with removing ActiveX (the VLAX- functions) and make it pure LISP please? That way it will work in AcCoreConsole.

Code: [Select]
;Code adapted from LeeMac's code at: http://www.theswamp.org/index.php?topic=43352.msg507568#msg507568
(defun c:EraseOutsideTitleBlock (/ SS BOX SS_ALL SS_KEEP LLP URP ENT idx flg tab)
  ;; Block Name  -  Lee Mac
  ;; Returns the true (effective) name of a supplied block reference
                       
  (defun LM:blockname ( obj )
      (if (vlax-property-available-p obj 'effectivename)
          (defun LM:blockname ( obj ) (vla-get-effectivename obj))
          (defun LM:blockname ( obj ) (vla-get-name obj))
      )
      (LM:blockname obj)
  )
 
  ;; Selection Set Bounding Box  -  Lee Mac
  ;; Returns a list of the lower-left and upper-right WCS coordinates of a
  ;; rectangular frame bounding all objects in a supplied selection set.
  ;; sel - [sel] Selection set for which to return bounding box

  (defun LM:ssboundingbox ( sel / idx llp ls1 ls2 obj urp )
      (repeat (setq idx (sslength sel))
          (setq obj (vlax-ename->vla-object (ssname sel (setq idx (1- idx)))))
          (if (and (vlax-method-applicable-p obj 'getboundingbox)
                  (not (vl-catch-all-error-p (vl-catch-all-apply 'vla-getboundingbox (list obj 'llp 'urp))))
              )
              (setq ls1 (mapcar 'min (vlax-safearray->list llp) (cond (ls1) ((vlax-safearray->list llp))))
                    ls2 (mapcar 'max (vlax-safearray->list urp) (cond (ls2) ((vlax-safearray->list urp))))
              )
          )
      )
      (if (and ls1 ls2) (list ls1 ls2))
  )
  (setq tab (getvar "ctab"))
  (if (setq SS (ssget "_X" (list '(0 . "INSERT") '(8 . "$TB") (cons 410 tab))))
    (progn
      (setq BOX (LM:ssboundingbox SS)
            LLP (car BOX)
            URP (cadr BOX)
            SS_ALL (ssget "_X" (list (cons 410 tab)))
            SS_KEEP (ssget "_W" (mapcar '- llp '(1e-2 1e-2)) (mapcar '+ urp '(1e-2 1e-2)) (list (cons 410 tab)))
      )
     
      (repeat (setq idx (sslength SS_all))
        (if
            (not
                (or (ssmemb (setq ent (ssname SS_all (setq idx (1- idx)))) SS_KEEP)
                    (or (and (= "INSERT" (cdr (assoc 0 (entget ent))))
                          (wcmatch (strcase (LM:blockname (vlax-ename->vla-object ent))) "PLOT STAMP*")
                        )
                        (= "$$tbinfo" (cdr (assoc 8 (entget ent))))
                        (= "$tbinfo" (cdr (assoc 8 (entget ent))))
                    )
                )
            )
            (progn
                (entdel ent)
                (or flg (setq flg (princ "\nObject(s) found outside the title block and will now be deleted.")))
            )
        )
      )   
    )
  )
)

BIGAL

  • Swamp Rat
  • Posts: 1396
  • 40 + years of using Autocad
Re: Erase Outside Title Block
« Reply #1 on: July 21, 2021, 09:33:37 PM »
The quick and dirty if you have a fixed size title block ours was always at 0,0 and 1:1 size then do a move window, way past the junk use extmax then erase the window of previous extmin extmax move your title block back all done.

Again if you have a title block scaled then you can still work out a move window of just the title block as you know its true size.
A man who never made a mistake never made anything

cmwade77

  • Swamp Rat
  • Posts: 1443
Re: Erase Outside Title Block
« Reply #2 on: July 22, 2021, 11:05:17 AM »
The quick and dirty if you have a fixed size title block ours was always at 0,0 and 1:1 size then do a move window, way past the junk use extmax then erase the window of previous extmin extmax move your title block back all done.

Again if you have a title block scaled then you can still work out a move window of just the title block as you know its true size.
A fixed size title block would be simple, unfortunately we have to use client title blocks that can range anywhere from 8.5x11 on up to 36x48, sometimes larger.

BIGAL

  • Swamp Rat
  • Posts: 1396
  • 40 + years of using Autocad
Re: Erase Outside Title Block
« Reply #3 on: July 22, 2021, 10:58:23 PM »
Can still be done as you know the name and the size of the title block, so can still get a window size and location. If they are blocks can also use bounding box to find the title window. You may just have to provide  title block name/s.
A man who never made a mistake never made anything

cmwade77

  • Swamp Rat
  • Posts: 1443
Re: Erase Outside Title Block
« Reply #4 on: July 23, 2021, 11:10:10 AM »
Can still be done as you know the name and the size of the title block, so can still get a window size and location. If they are blocks can also use bounding box to find the title window. You may just have to provide  title block name/s.
That is the issue, programmatically, I don't have anyway to know what size the title block is that I can think of that doesn't involve ActiveX. If I could read page setups without ActiveX, that might be a way around that though.

ronjonp

  • Needs a day job
  • Posts: 7526
Re: Erase Outside Title Block
« Reply #5 on: July 23, 2021, 12:57:54 PM »
Can still be done as you know the name and the size of the title block, so can still get a window size and location. If they are blocks can also use bounding box to find the title window. You may just have to provide  title block name/s.
That is the issue, programmatically, I don't have anyway to know what size the title block is that I can think of that doesn't involve ActiveX. If I could read page setups without ActiveX, that might be a way around that though.
Maybe you can use this: (dictsearch (namedobjdict) "ACAD_LAYOUT")

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

cmwade77

  • Swamp Rat
  • Posts: 1443
Re: Erase Outside Title Block
« Reply #6 on: July 23, 2021, 01:05:42 PM »
I have something that seems to be working, perhaps some people can kick the tires as it were to let me know where issues may exist?

Code: [Select]
;Code adapted from LeeMac's code at: http://www.theswamp.org/index.php?topic=43352.msg507568#msg507568
(defun EraseOutsideTitleBlock (/ SS BOX SS_ALL SS_KEEP LLP URP ENT idx flg tab)
 
  (defun DTR (a) ;degrees to radians function
    (* PI (/ a 180.0))
  );defun
 
  (defun CW:BoundingBox (ss / psize i EntType LLP URP PT2)
    ; get paper size on current tab - Adapted from JTB World - https://jtbworld.com/autocad-pagesetup-lsp
    (defun papersize (/ psn scale)
      (setq
        psn (member '(100 . "AcDbPlotSettings")
        (dictsearch
          (cdr (assoc -1 (dictsearch (namedobjdict) "ACAD_LAYOUT")))
          (getvar "ctab")
        )
      )
      )
      (if (= (caadr psn) 1) ; Page Setup Name exist
        (progn (setq scale (if (= 0 (cdr (assoc 72 psn)))
          25.4
          1.0
              )
        )
        (list (/ (cdr (assoc 45 psn)) scale) (/ (cdr (assoc 44 psn)) scale))
       
        )
      )
    )
   
    (setq psize (papersize))
    (if psize
      (progn
        (repeat (setq i (sslength ss))
          (setq ent (entget (ssname ss (setq i (1- i))))
                EntType (cdr (assoc 0 ent))
                LLP (cdr (assoc 10 ent))
                PT2 (polar LLP (DTR 90.0) (cadr psize))
                URP (polar PT2 (DTR 0.0) (car psize))
          )
         
        )
      )
    )
    (if (and llp urp)
      (list LLP URP)
    )
  )
 
 
  (setq tab (getvar "ctab"))
  (if (setq SS (ssget "_X" (list '(0 . "INSERT") '(8 . "$TB") (cons 410 tab))))
    (progn
      (setq BOX (CW:BoundingBox SS)
            LLP (car BOX)
            URP (cadr BOX)
            SS_ALL (ssget "_X" (list (cons 410 tab)))
            SS_KEEP (ssget "_W" (mapcar '- llp '(1e-2 1e-2)) (mapcar '+ urp '(1e-2 1e-2)) (list (cons 410 tab)))
      )
      (if (and (/= SS_ALL nil) (/= SS_KEEP nil))
        (progn
          (command "._-layer" "_unlock" "*" "")
          (repeat (setq idx (sslength SS_all))
            (if
                (not
                    (or (ssmemb (setq ent (ssname SS_all (setq idx (1- idx)))) SS_KEEP)
                        (or
                            (= "$$tbinfo" (cdr (assoc 8 (entget ent))))
                            (= "$tbinfo" (cdr (assoc 8 (entget ent))))
                        )
                    )
                )
                (progn
                    (entdel ent)
                    (or flg (setq flg (princ "\nObject(s) found outside the title block and will now be deleted.")))
                )
            )
          )
        )
      )
    )
  )
  (princ)
)

BIGAL

  • Swamp Rat
  • Posts: 1396
  • 40 + years of using Autocad
Re: Erase Outside Title Block
« Reply #7 on: July 25, 2021, 09:41:48 PM »
I still think it can be done easier again I ask do you have a title block that encloses the area you want to keep ? The scale etc does not matter as the bounding box will give size. then just move that window out of harms way why you erase the junk.

If you dont have a title block draw a rectang on a non plot layer, its really about some form of dwg standards so clean up can occur.
A man who never made a mistake never made anything

cmwade77

  • Swamp Rat
  • Posts: 1443
Re: Erase Outside Title Block
« Reply #8 on: July 26, 2021, 11:23:04 AM »
I still think it can be done easier again I ask do you have a title block that encloses the area you want to keep ? The scale etc does not matter as the bounding box will give size. then just move that window out of harms way why you erase the junk.

If you dont have a title block draw a rectang on a non plot layer, its really about some form of dwg standards so clean up can occur.
You can't get a bounding box through the AcCoreConsole.exe, as it requires ActiveX to do so and there are actually a few reasons for the erasing of items outside the title block:
  • Items outside the title block cause issues with plotting
  • Items outside the title block are unsightly and not within our company standards
  • Items outside the title block tends to cause the CAD files to run slower
  • Items outside the title block tends to cause the CAD files to become corrupted more often.
  • When we do need to send CAD files out, we do not want the items outside the title block as it doesn't look professional.

NOTE: I am also working on a Reactor and that will use the bounding box method.

If it were strictly the plotting, I could get the bounding box and use that as a window for the plot, but there are the other issues as well. Hopefully this explains more about why the approach of erasing the objects is the approach we are taking.
« Last Edit: July 26, 2021, 11:28:54 AM by cmwade77 »

BIGAL

  • Swamp Rat
  • Posts: 1396
  • 40 + years of using Autocad
Re: Erase Outside Title Block
« Reply #9 on: July 26, 2021, 08:16:12 PM »
You say stuff outside affects plotting, we had a very strict rule the title block was at 0,0 no ifs or buts, also had a move all paperspace so title is 0,0 for those who screwed it up. Plotting using a window option and centre at scale we used this for years 100% success rate. Our dwgs had junk at times next to the title block.

Code: [Select]
(COMMAND "-PLOT"  "Y"  "" "dwg to Pdf"
       "Iso full bleed A3 (420.00 x 297.00 MM)" "m" "LANDSCAPE"  "N"   "W"  "-6,-6" "807,560" "1=2"  "C"
       "y" "Designlasercolour.ctb" "Y" "n" "n" "n" pdfName "N" "y"
    )
A man who never made a mistake never made anything

BIGAL

  • Swamp Rat
  • Posts: 1396
  • 40 + years of using Autocad
Re: Erase Outside Title Block
« Reply #10 on: July 26, 2021, 08:23:05 PM »
I still think this is doable no VL.

A man who never made a mistake never made anything

cmwade77

  • Swamp Rat
  • Posts: 1443
Re: Erase Outside Title Block
« Reply #11 on: July 27, 2021, 11:27:31 AM »
I still think this is doable no VL.


Ok, but how would I get the boundary of the title block without VL? Keep in mind, the title block can be any size from 8.5x11 to I believe our largest is 48x52 and all are named identically and there isn't always a box around them and going back and modifying them isn't an option.

I also have a ton of stuff in paperspace that has to stay, so I can't just erase everything outside the moved title block.
« Last Edit: July 27, 2021, 11:41:02 AM by cmwade77 »

BIGAL

  • Swamp Rat
  • Posts: 1396
  • 40 + years of using Autocad
Re: Erase Outside Title Block
« Reply #12 on: July 27, 2021, 11:21:04 PM »
Ok if its a block has a scale answers size problem.

Your contradicting your request "I also have a ton of stuff in paperspace that has to stay, so I can't just erase everything outside the moved title block."

Sounds like company standards need to be adhered to. There should not be any junk in any layout.

It sounds like the end for me.

Good luck in trying to find something as a solution.
A man who never made a mistake never made anything

ronjonp

  • Needs a day job
  • Posts: 7526
Re: Erase Outside Title Block
« Reply #13 on: July 28, 2021, 11:43:35 AM »
I still think this is doable no VL.


Ok, but how would I get the boundary of the title block without VL? Keep in mind, the title block can be any size from 8.5x11 to I believe our largest is 48x52 and all are named identically and there isn't always a box around them and going back and modifying them isn't an option.

I also have a ton of stuff in paperspace that has to stay, so I can't just erase everything outside the moved title block.
I understand the OCD of not wanting to have stuff outside of the titleblock .. I have it too.  :-D If you plot to VIEW you don't have to worry about the stuff outside though. I've never seen files that have been 'corrupted' from having stuff outside of plotting extents?

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

cmwade77

  • Swamp Rat
  • Posts: 1443
Re: Erase Outside Title Block
« Reply #14 on: July 28, 2021, 12:35:09 PM »
I am not contradicting anything, I think you are misunderstanding:
  • We have a title block that is say 30"x42" (can be any size though, even nonstandard sizes and there is no way to tell from a block name)
  • In paper space, our company standard is to draw: Most dimensions, detail boxes, details that aren't to scale, general notes, reference notes, title block information, such as sheet name, number, scale and other items that someone wouldn't need to see if they were xrefing our drawings in as a background. Some dimensions, like duct size would be drawn in modelspace, as someone might need that information.

Our standard is to not draw anything outside the boundary of the title block's space (in this case the 30"x42" boundary); unfortunately sometimes people do so.

I have attached a very simplistic example for clarity. Please note that sensitive data has been erased, so if looks like some things are missing, they are.