TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: cmwade77 on July 21, 2021, 06:09:21 PM

Title: Erase Outside Title Block
Post by: cmwade77 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.")))
            )
        )
      )   
    )
  )
)
Title: Re: Erase Outside Title Block
Post by: BIGAL 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.
Title: Re: Erase Outside Title Block
Post by: cmwade77 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.
Title: Re: Erase Outside Title Block
Post by: BIGAL 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.
Title: Re: Erase Outside Title Block
Post by: cmwade77 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.
Title: Re: Erase Outside Title Block
Post by: ronjonp 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")
Title: Re: Erase Outside Title Block
Post by: cmwade77 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)
)
Title: Re: Erase Outside Title Block
Post by: BIGAL 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.
Title: Re: Erase Outside Title Block
Post by: cmwade77 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:

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.
Title: Re: Erase Outside Title Block
Post by: BIGAL 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"
    )
Title: Re: Erase Outside Title Block
Post by: BIGAL on July 26, 2021, 08:23:05 PM
I still think this is doable no VL.

(http://)
Title: Re: Erase Outside Title Block
Post by: cmwade77 on July 27, 2021, 11:27:31 AM
I still think this is doable no VL.

(http://)
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.
Title: Re: Erase Outside Title Block
Post by: BIGAL 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.
Title: Re: Erase Outside Title Block
Post by: ronjonp on July 28, 2021, 11:43:35 AM
I still think this is doable no VL.

(http://)
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?
Title: Re: Erase Outside Title Block
Post by: cmwade77 on July 28, 2021, 12:35:09 PM
I am not contradicting anything, I think you are misunderstanding:

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.
Title: Re: Erase Outside Title Block
Post by: cmwade77 on July 28, 2021, 12:46:51 PM
I still think this is doable no VL.

(http://)
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?

Unfortunately, that assumes that view is always setup correctly, which isn't always the case. If it were just me working on drawings, it wouldn't be a big deal, I could ensure that everything is setup correctly, but I am a single CAD manger trying to support and manage 73 AutoCAD users at present, across two countries. As a result, I pretty much need to do everything I can to idiot proof things.

And unfortunately, upper management often makes this even worse when they insist on hiring people that don't know AutoCAD at all (because they are good with the engineering) and expects that we can train them overnight in how to use AutoCAD, I keep trying to tell them it doesn't work that way.

So again, I am left with idiot proofing these things and even the page layouts aren't always setup correctly for plotting, so I can't just use layout, extents really is the ideal choice after a lot of extensive testing.

As far as corruption, we have had some drawings be corrupted due to objects (generally AEC objects, which is another pain point) outside the plotting extents and if you can get the file opened and move them back within the plotting extents, the file is fine.
Title: Re: Erase Outside Title Block
Post by: BIGAL on July 29, 2021, 12:36:10 AM
Company standards comes to mind sounds like that is your first step.
Title: Re: Erase Outside Title Block
Post by: ronjonp on July 29, 2021, 10:37:46 AM
I still think this is doable no VL.

(http://)
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?

Unfortunately, that assumes that view is always setup correctly, which isn't always the case. If it were just me working on drawings, it wouldn't be a big deal, I could ensure that everything is setup correctly, but I am a single CAD manger trying to support and manage 73 AutoCAD users at present, across two countries. As a result, I pretty much need to do everything I can to idiot proof things.

And unfortunately, upper management often makes this even worse when they insist on hiring people that don't know AutoCAD at all (because they are good with the engineering) and expects that we can train them overnight in how to use AutoCAD, I keep trying to tell them it doesn't work that way.

So again, I am left with idiot proofing these things and even the page layouts aren't always setup correctly for plotting, so I can't just use layout, extents really is the ideal choice after a lot of extensive testing.

As far as corruption, we have had some drawings be corrupted due to objects (generally AEC objects, which is another pain point) outside the plotting extents and if you can get the file opened and move them back within the plotting extents, the file is fine.
Sounds like you have your hands full! Have you thought about processing these files with ODBX ?
Title: Re: Erase Outside Title Block
Post by: cmwade77 on July 29, 2021, 11:29:18 AM
Company standards comes to mind sounds like that is your first step.
Hey, we are a far cry better than it used to be, I will take the baby steps, at least I have number bubbles in reference and general notes lining up and sheet indexes reflecting the correct deltas, etc.
Title: Re: Erase Outside Title Block
Post by: cmwade77 on July 29, 2021, 11:31:39 AM
I still think this is doable no VL.

(http://)
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?

Unfortunately, that assumes that view is always setup correctly, which isn't always the case. If it were just me working on drawings, it wouldn't be a big deal, I could ensure that everything is setup correctly, but I am a single CAD manger trying to support and manage 73 AutoCAD users at present, across two countries. As a result, I pretty much need to do everything I can to idiot proof things.

And unfortunately, upper management often makes this even worse when they insist on hiring people that don't know AutoCAD at all (because they are good with the engineering) and expects that we can train them overnight in how to use AutoCAD, I keep trying to tell them it doesn't work that way.

So again, I am left with idiot proofing these things and even the page layouts aren't always setup correctly for plotting, so I can't just use layout, extents really is the ideal choice after a lot of extensive testing.

As far as corruption, we have had some drawings be corrupted due to objects (generally AEC objects, which is another pain point) outside the plotting extents and if you can get the file opened and move them back within the plotting extents, the file is fine.
Sounds like you have your hands full! Have you thought about processing these files with ODBX ?
I actually already do some processing with OBX, not sure how I would do this using OBX though, but that would be awesome.
Title: Re: Erase Outside Title Block
Post by: BIGAL on August 02, 2021, 12:40:10 AM
There is some good posts around about creating company standards what to look for do a google, sounds like the 1st step, new employee here is our rules you must use. Maybe forums/autodesk.

I previously had some interesting input from a Cad manager multi millions $$ project 200 + building sites revamp of corporate appearance the Cad manager removed the standard menu and replaced with a custom one that had draw objects, not lines, so always correct, that was a outsourced project to a cheaper country where he went to live for 6 months to ensure company standards.
Title: Re: Erase Outside Title Block
Post by: cmwade77 on August 04, 2021, 12:29:31 PM
There is some good posts around about creating company standards what to look for do a google, sounds like the 1st step, new employee here is our rules you must use. Maybe forums/autodesk.

I previously had some interesting input from a Cad manager multi millions $$ project 200 + building sites revamp of corporate appearance the Cad manager removed the standard menu and replaced with a custom one that had draw objects, not lines, so always correct, that was a outsourced project to a cheaper country where he went to live for 6 months to ensure company standards.
Again, we have CAD Standards, that isn't the issue, unfortunately, not everyone follows the CAD standards all of the time, even though they know the rules. I try my best to enforce them, but I can only do so much. Simply put, this is NOT about our CAD Standards, but how to deal with people drawing outside of title blocks. We also run into this issue with files sent to us from other companies that we have to use as xrefs and that also causes a lot of issues, so having a handy way to clean it up rapidly is worthwhile.
Title: Re: Erase Outside Title Block
Post by: ronjonp on August 04, 2021, 06:52:05 PM
How can you rely on a layer filter for your titleblocks if users are unruly? There could be other items on that layer too?
Code: [Select]
(setq SS (ssget "_X" (list '(0 . "INSERT") '(8 . "$TB") (cons 410 tab))))
Title: Re: Erase Outside Title Block
Post by: ronjonp on August 05, 2021, 10:37:58 AM
Here's a start of some code that could be adapted for ODBX. Enjoy!
Code - Auto/Visual Lisp: [Select]
  1. (defun c:cleanlayout (/ a b e r ll tb ur x1 x2 y1 y2)
  2.   ;; RJP » 2021-08-05
  3.   ;; Deletes all objects not found within extents of titleblock on all paperspace tabs
  4.   (if (and (setvar 'tilemode 0)
  5.            (setq e (car (entsel "\nPick titleblock to keep: ")))
  6.            (= "INSERT" (cdr (assoc 0 (entget e))))
  7.            (setq e (cdr (assoc 2 (entget e))))
  8.       )
  9.     (progn
  10.         (cond ((and (= -1 (vlax-get b 'islayout)) (not (wcmatch (vlax-get b 'name) "`*Model*")))
  11.                (setqnil
  12.                      tb nil
  13.                )
  14.                (vlax-for a b
  15.                  (vla-getboundingbox a 'll 'ur)
  16.                  (mapcar 'set '(ll ur) (mapcar 'vlax-safearray->list (list ll ur)))
  17.                  (if (and (vlax-property-available-p a 'name) (wcmatch (vla-get-name a) e))
  18.                    (setq tb (list ll ur))
  19.                    (or (= "AcDbViewport" (vla-get-objectname a)) (setq r (cons (list ll ur a) r)))
  20.                  )
  21.                )
  22.                (if tb
  23.                  (progn (setq x1 (caar tb)
  24.                               x2 (caadr tb)
  25.                               y1 (cadar tb)
  26.                               y2 (cadr (cadr tb))
  27.                         )
  28.                         (foreach o r
  29.                           (setq a (mapcar 'car (list (car o) (cadr o))))
  30.                           (setq b (mapcar 'cadr (list (car o) (cadr o))))
  31.                           (or (and (apply '<= (list x1 (car a) (cadr a) x2))
  32.                                    (apply '<= (list y1 (car b) (cadr b) y2))
  33.                               )
  34.                               (vla-delete (caddr o))
  35.                           )
  36.                         )
  37.                  )
  38.                )
  39.               )
  40.         )
  41.       )
  42.     )
  43.   )
  44.   (princ)
  45. )