Jump to content

Find and delete odd numbers


SWfangirl

Recommended Posts

I need some help. I have a drawing that has 2000+ utility poles in it. All of the poles are labed numerically using mtext. Is there a way to select just the odd values and delete them?

Link to comment
Share on other sites

Welcome to CADTutor SWfangirl :)

 

Perhaps try something like this:

 

(defun c:DelOdd ( / ss i e )

 (if (setq ss (ssget "_X" '((0 . "MTEXT") (8 . "[color=red]YourLayerHere[/color]"))))
   (repeat (setq i (sslength ss))
     (setq e (ssname ss (setq i (1- i))))
     (if (= 1 (logand 1 (atoi (cdr (assoc 1 (entget e)))))) (entdel e))
   )
 )
 (princ)
)

The above assumes the labels are integers and that the MText is unformatted and its string just contains a number and no other text.

 

I have also added a layer filter to the Selection to only select MText on a certain layer, change the layer name (marked in red) to your MText Layer, or remove the (8 . "XX") part to select all MText.

Link to comment
Share on other sites

Or this might be better:

 

(defun c:DelOdd ( / ss i e )

 (if (setq ss (ssget "_X" '((0 . "MTEXT"))))
   (repeat (setq i (sslength ss))
     (setq e (ssname ss (setq i (1- i))))
     (if (zerop (logand 1 (atoi (cdr (assoc 1 (entget e)))))) (ssdel e ss))
   )
 )
 (sssetfirst nil ss) (princ)
)

 

Will just select the 'Odd' MText so that you can delete them yourself.

Link to comment
Share on other sites

Are you trying to delete just the mtext label or is there some geometry ( ie circle ) that need to removed as well? That could be much more complicated. -David

Link to comment
Share on other sites

How do you work (= (rem i 2) 1) into the lisp? :oops:

 

An example using my previous code:

 

(defun c:DelOdd ( / ss i e )

 (if (setq ss (ssget "_X" '((0 . "MTEXT"))))
   (repeat (setq i (sslength ss))
     (setq e (ssname ss (setq i (1- i))))
     (if (zerop (rem (abs (atoi (cdr (assoc 1 (entget e))))) 2)) (ssdel e ss))
   )
 )
 (sssetfirst nil ss) (princ)
)

Link to comment
Share on other sites

Or maybe a bit more elementary:

 

[b][color=BLACK]([/color][/b]defun c:doddtxt [b][color=FUCHSIA]([/color][/b]/ ss en ed tv ti[b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]and [b][color=NAVY]([/color][/b]setq ss [b][color=MAROON]([/color][/b]ssget [color=#2f4f4f]"X"[/color] '[b][color=GREEN]([/color][/b][b][color=BLUE]([/color][/b]0 . [color=#2f4f4f]"*TEXT"[/color][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b] [color=#8b4513];;;CREATE A PICKSET OF ALL TEXT ENTITIES[/color]
      [b][color=NAVY]([/color][/b]while [b][color=MAROON]([/color][/b]setq en [b][color=GREEN]([/color][/b]ssname ss 0[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]         [color=#8b4513];;;WHILE THERE IS AN ENTITY NAME LEFT IN THE SET, GET THE FIRST ENAME[/color]
             [b][color=MAROON]([/color][/b]setq ed [b][color=GREEN]([/color][/b]entget en[b][color=GREEN])[/color][/b]            [color=#8b4513];;;GET ENTITY DEFINTION[/color]
                   tv [b][color=GREEN]([/color][/b]cdr [b][color=BLUE]([/color][/b]assoc 1 ed[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b]     [color=#8b4513];;;GET THE ENTITY TEXT VALUE[/color]
                   ti [b][color=GREEN]([/color][/b]atoi tv[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]             [color=#8b4513];;;CONVERT THE TEXT VALUE TO AN INTEGER[/color]
              [b][color=MAROON]([/color][/b]if [b][color=GREEN]([/color][/b]/= [b][color=BLUE]([/color][/b]rem ti 2[b][color=BLUE])[/color][/b] 0[b][color=GREEN])[/color][/b]          [color=#8b4513];;;IF THE INTEGER IS NOT EVENLY DIVISIBLE BY 2 ( NOT EVEN )[/color]
                  [b][color=GREEN]([/color][/b]entdel en[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]               [color=#8b4513];;;DELETE ENTITY[/color]
              [b][color=MAROON]([/color][/b]ssdel en ss[b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]                [color=#8b4513];;;REMOVE THE ENAME FROM THE PICKSET[/color]
  [b][color=FUCHSIA]([/color][/b]prin1[b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b]

 

 

-David

Edited by David Bethel
Link to comment
Share on other sites

Thank you David,

It really helps seeing what the code means.

It is much easier to grasp this way.

Trying to learn lisp is exciting and aggravating all at the same time.:?

Edited by SWfangirl
Fixed spelling error
Link to comment
Share on other sites

You're welcome. Sometimes we get a bit advanced for a beginner and forget how we learned best way back when. -David

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...