TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: V-Man on October 14, 2004, 05:13:19 PM

Title: Check Dwg for Mirrored blocks
Post by: V-Man on October 14, 2004, 05:13:19 PM
I have this code to go through the entire drawing to check for any block that is mirrored and if any draw a circle from the insertion point of any blocks found. My question is, is there a better way or another way this can be written? Also, I would like this to report via command prompt how many it found.


Code: [Select]

(defun c:mblk ()
  (setq cmd (getvar "CMDECHO"))
  (setvar "CMDECHO" 0)
  (command ".layer" "s" "0" "")
  (setq savosmode (getvar "OSMODE"))
  (command ".layer" "on" "0" "thaw" "0" "unlock" "0" "")
  (setvar "osmode" 0)
  (setq blks (ssget "x" '((0 . "INSERT"))))
  (setq count (sslength blks))
  (setq count (sslength blks))
  (setq index 0)
  (repeat count
    (setq b1 (entget (ssname blks index)))
    (setq b2 (cdr(assoc 41 b1)))
    (setq b3 (rtos b2 2 0))
    (setq b4 (substr b3 1 1))
    (if (= b4 "-")
      (progn
 (setq c1 (cdr(assoc 10 b1)))
 (command "circle" c1 50)
 );end progn
      );end if
    (setq index (+ index 1))
    );end repeat
  (setvar "CMDECHO" cmd)
  (setvar "OSMODE" savosmode)
  (princ)
)
Title: Check Dwg for Mirrored blocks
Post by: MP on October 14, 2004, 05:50:55 PM
In my opinion it is better to separate a solution to a particular problem into logical functions, each function doing exactly one thing (and one thing only), and then the program, which is a collection of functions, solves the problem. Not only does it make it far easier to analyze the problem and distribute the responsibility of the "work" logically, it is a cornerstone of code re-use.

In this case I see a total of 4 functions: one to find mirrored inserts, one to cycle thru a selection set and apply an affect to the members of that selection set, one that applies a specific effect upon an entity and then finally a c:function which calls the afore mentioned functions comprising "the solution" appropriately such that the problem is solved and accessible to the user as a command line utility.

Passing batton ...

Edit: Removed superfluous code.
Title: Check Dwg for Mirrored blocks
Post by: SMadsen on October 14, 2004, 05:51:07 PM
You can grab all mirrored block by adding a relational filter:

(setq sset (ssget "X" '((0 . "INSERT")(-4 . "<")(41 . 0))))

This will only include inserts that has an x-scale factor lesser than 0.

*didn't see Mr. Puckett sneak in there*
Title: Check Dwg for Mirrored blocks
Post by: MP on October 14, 2004, 05:55:02 PM
<cough>, that's what I meant. :)
Title: Check Dwg for Mirrored blocks
Post by: CAB on October 14, 2004, 08:35:10 PM
and if you want to unmirror :)
http://theswamp.org/phpBB2/viewtopic.php?t=2064&highlight=mirrored
Title: Check Dwg for Mirrored blocks
Post by: V-Man on October 15, 2004, 08:53:41 AM
Ok, here is my revised code. Thanks to everyone.

Code: [Select]

(defun c:Mblk ()
(setq cmd (getvar "CMDECHO"))
(setvar "CMDECHO" 0)
(command ".layer" "on" "0" "thaw" "0" "unlock" "0" "")
(command ".layer" "s" "0" "")
(setq savosmode (getvar "OSMODE"))
(setvar "osmode" 0)
(setq sset (ssget "X" '((0 . "INSERT")(-4 . "<")(41 . 0))))
    (if (/= sset nil)
      (progn
      (setq i (sslength sset))
      (setq index 0)
(repeat i
(setq b1 (entget (ssname sset index)))
(setq c1 (cdr(assoc 10 b1)));;find block insertion
(command "circle" c1 50);;<----Draw a circle from block insertion
(setq index (+ index 1))
)
(prompt (strcat "\n*-* " (itoa i) " - Mirrored Blocks found!  Please FIX....*-*"))
      )
(prompt "No Mirrored Blocks Found! Have a nice day....")
)
(setvar "CMDECHO" cmd)
(setvar "OSMODE" savosmode)
(princ)
)


The purpose is to locate any blocks such as doors, toilet fixtures etc... and locate them on the floor plan so that the user can go in and fix. I like the un-mirror lisp as well but if a door is inserted in place and then mirrored then un-mirroring it will of course flip the door, hence it will be incorrect door swing and location.

The end result is BLOCKS should never (in my opinion) be MIRROR'ed. This will cause problems for anyone in the future revising that block.

My 2 cents...

Thanks again guys..
Title: Check Dwg for Mirrored blocks
Post by: SMadsen on October 15, 2004, 09:01:56 AM
Heh what a polite prompt ..
Code looks good.