Detect if objects in a layer exist (and then perform a task)

Detect if objects in a layer exist (and then perform a task)

Anonymous
Not applicable
2,769 Views
19 Replies
Message 1 of 20

Detect if objects in a layer exist (and then perform a task)

Anonymous
Not applicable

I have (sadly) been separated from AutoCAD for over 10 years, so I'm very rusty.  I have 9,000 drawings that need various alterations and, with the help of these forums, I have managed to achieve most of the tasks.  However, there a few remaining that I haven't been able to solve.  I would be grateful for any guidance.

 

I need to determine if there are any objects (commonly lines or polylines) in a particular layer.  If there are, then I need to add text "statement A" and if not, "statement B".

 

I'm using AutoCAD 2020.  

 

0 Likes
2,770 Views
19 Replies
Replies (19)
Message 2 of 20

pbejse
Mentor
Mentor

@Anonymous wrote:

..I need to determine if there are any objects (commonly lines or polylines) in a particular layer.  If there are, then I need to add text "statement A" and if not, "statement B".


Sounds easy enough, where would that statement be added to?  Layer name? the drawing file? Title block? or on the object? if yes, at the midpoint and the end? at the start of the line? etc...

 

0 Likes
Message 3 of 20

Anonymous
Not applicable

Thank you for your reply.

There is an existing MTEXT object (only one in the drawing) which contains a property name (on top line) and a number (on the second line).  

 

BRIGHT WATER  (property name)

10102345 (number)

 

The statement text (either A or B) is to be added to this MTEXT and on the third line with the same justification.  (Presumably, adding the text to an existing item automatically deals with the layer.)  Ideally the statement text will have a smaller text size than the two other lines, and a spacing of 0.8x for all three lines.  I've attached an original drawing and a desired solution PDF.  

 

BRIGHT WATER

10102345

City of Regency    (statement text)

 

Does that help?

0 Likes
Message 4 of 20

pbejse
Mentor
Mentor

@Anonymous wrote:

Thank you for your reply.

There is an existing MTEXT object (only one in the drawing) which contains a property name (on top line) and a number (on the second line).  

 

Does that help?


Yes it does help, what are the conditions for the statement for A or B? what layer goes with what statement

"Layer1" = string statement A

"Layer2"= string statement  B

or possbily 

"Layer3"= string statement  C

and so on...

 

Are the target lines or polylines on Model Space

What if both of the layers exists?

 

0 Likes
Message 5 of 20

Anonymous
Not applicable

Oh thank you!

 

If there is an object in (say) LAYER1 then STATEMENT A is to be added.

If there is no object in LAYER1 (that is any objects in any other layers) then STATEMENT B is added.  The routine only needs to test LAYER1.

 

LAYER1 may be in the drawing, but not have been used (or purged).  It is only if there is an object actually in the layer. 

 

If I could trust the drafters then I could confirm that the lines or plines are in Model Space only....but I can't.  They could be in either Paper or Model Space.  The MTEXT is in Paper Space.

0 Likes
Message 6 of 20

pbejse
Mentor
Mentor

@Anonymous wrote:

Oh thank you!

 

If there is an object in (say) LAYER1 then STATEMENT A is to be added.

If there is no object in LAYER1 (that is any objects in any other layers) then STATEMENT B is added.  The routine only needs to test LAYER1.

 

LAYER1 may be in the drawing, but not have been used (or purged).  It is only if there is an object actually in the layer. 

 

If I could trust the drafters then I could confirm that the lines or plines are in Model Space only....but I can't.  They could be in either Paper or Model Space.  The MTEXT is in Paper Space.


 

Ok, if you prefer not to post the actual name and statement , i will just post a generic routine here, and you change the layer name and statement you need to add  whatever it is.

 

Routine needs to read 9000 drawings.

First thing the routine will do is to purge the file. [purging will remove unused layers]

Check for layers:

If exist, find the target MTEXT and apply statement 1 else statement 2

Correct?

 

 

 

 

 

 

0 Likes
Message 7 of 20

Anonymous
Not applicable

Yes, correct.  However, I'd rather that the layer(s) aren't purged, as individual drawing(s) may need to use LAYER1 at a later stage.  I can't trust or expect the users to remake LAYER1.  So, the test is there an object in the drawing in LAYER1; not merely is LAYER1 in the drawing. 

 

I suppose one could try to PURGE it and if successful it means that there are no objects in LAYER1.  Before leaving the drawing LAYER1 could be restored. 

0 Likes
Message 8 of 20

Kent1Cooper
Consultant
Consultant

Unless it's possible that something would be on that Layer nested inside a Block only, but nothing at top level, the whether-any-object-on-Layer part is easy:

 

(if (ssget "_X" '(8 . "LAYER1"))

  (…. then -- add Statement 1 ….)

  (…. else -- add Statement 2 ....)

)

 

The adding of the statement is more complicated, but I can't spend time on that now -- maybe later if no one else jumps in.

Kent Cooper, AIA
0 Likes
Message 9 of 20

pbejse
Mentor
Mentor

@Anonymous wrote:

Yes, correct.  However, I'd rather that the layer(s) aren't purged, as individual drawing(s) may need to use LAYER1 at a later stage.  I can't trust or expect the users to remake LAYER1.  So, the test is there an object in the drawing in LAYER1; not merely is LAYER1 in the drawing. 

 

I suppose one could try to PURGE it and if successful it means that there are no objects in LAYER1.  Before leaving the drawing LAYER1 could be restored. 


 

"STATEMENT 1" if and only if the layer is assigned to any object: Else  STATEMENT 2:

means "STATEMENT 2" if the layer does exist but not assigned to any object:

AND if it does not exist dont even bother creating the LAYER on the drawing.

 

 

 

0 Likes
Message 10 of 20

pbejse
Mentor
Mentor

Its almost done, I only need now to cofirm How to Identify the Target Mtext on Paper Space.

On your sample drawing, we can use either of the the properties listed below.

  • String Value  : preferably the first line followed by a start (*) [ on your example "BRIGHT WATER*"
  • Mtext Height Value : 13.00
  • Layer: "Default"

Issue here is , i am not sure which of these properties are constant, we need at the least a minimum of two (2) of the MTEXT properties.

 

The way we will construct the STATEMENT values

 

(list
              (strcat folder "\\" itm)             
;;              the layer name to search for                    ;;
      
                        "Chocolate"       ;; Layer name to check if used on the selected drawings
              
;;            The new string assign to target MTEXT             ;;
;;          The first one being the layer does exist            ;;
;;            The string includes the formatting                ;;
              
;;       Statement IF Layer is assigned to an object            ;;
      
(list
     (Strcat               "BRIGHT WATER" ; This is where you place the first line of target Mtext
       "\\P"               "10102345"     ; This is where you place the Second line of target Mtext
       "\\P{\\H0.38462x;"  "STATEMENT 1"  ; This is the Additional string when Layer is used
       "}")

;;       Statement IF Layer is NOT assigned to an object        ;;

(list
     (Strcat               "BRIGHT WATER" ; This is where you place the first line of target Mtext
       "\\P"               "10102345"     ; This is where you place the Second line of target Mtext
       "\\P{\\H0.38462x;"  "STATEMENT 2"   ; This is the Additional string for Layer does not exist/used
       "}")
		)
              
;;    This is the data of the Target MTEXT                      ;;
;;    The program will look for this on only Paper space        ;;
;;    it will search for a part of the string                   ;;
;;    in this case "BRIGHT WATER" followed by a wildcard        ;;
;;    and the height of 13.00 units                             ;;
              
      (setq data '("BRIGHT WATER*"      ;; Mtext String value to identify the Target String
                   13.00                ;; Mtext height value to identify the Target String
                   "Default"))          ;; Mtext height layer to identify the Target String
      )

 

 

Either we hard code the values [ I think this is just a one time thing ] or the user can be prompted for.

  • Common First Line
  • Common Second Line
  • IF statement else Statement 2

When we settle all this issues, you can run the code on ALL 9000 drawings and not graphically open a single file.

Your reply to this I  gues will depend on how badly you need this routine, the solution is just right around the corner 🙂

 

Cheers

 

0 Likes
Message 11 of 20

Anonymous
Not applicable

Thank you so much.  This looks pretty close.  The property name is different for each drawing, and I don't know the name without opening each file.  So testing for the number, which I can supply and is different for each drawing, the layer (DEFAULT) and the text height (13.0) is the best choice.

 

I have a number of routines to run on each drawing so I'm quite captivated by your suggestion that the routines can be run without graphically opening the files.  The plan is to set this running and with minimal observation and no user input (until it fails!!).  I eagerly look forward to your advice.

0 Likes
Message 12 of 20

Anonymous
Not applicable

Kent, thank you for your input.  I think we're close to solving the issue, so you can attend to other troubled souls. [I am filing your instructions for future use.]

0 Likes
Message 13 of 20

Anonymous
Not applicable

Sorry, I forgot to say that it is only a one-time thing.  If it fails on any drawings it can be done manually.  The file/property number will come from a list so the routine probably needs a variable into which to feed the number.  Layer and height reasonably constant (I hope).

 

Ta! 

0 Likes
Message 14 of 20

john.uhden
Mentor
Mentor

According to what I've read (or perhaps misinterpreted) it doesn't matter whether the object is Mtext or anything else.

So simply...

(if (ssget "x" (list (cons 8 YourLayer)))

  (statement "A")

  (statement "B")

)

BUT, you didn't indicate if the object might be nested in a block.  That would require a different approach.

John F. Uhden

0 Likes
Message 15 of 20

pbejse
Mentor
Mentor

@colleen.dunn wrote:

Thank you so much.  This looks pretty close.  The property name is different for each drawing, and I don't know the name without opening each file.  So testing for the number, which I can supply and is different for each drawing, the layer (DEFAULT) and the text height (13.0) is the best choice.


Yes I'm counitng on you to say that, we can do away with the numbers then. We will go with the layer and height as identifier.

 


@colleen.dunn wrote:

I have a number of routines to run on each drawing so I'm quite captivated by your suggestion that the routines can be run without graphically opening the files.  

--------

Sorry, I forgot to say that it is only a one-time thing.  If it fails on any drawings it can be done manually.  The file/property number will come from a list so the routine probably needs a variable into which to feed the number.  


Its nothing new really, we do it all the time, i have not cosider the fact  you will be running more than just one routine, Since you will be running a script, I guess we could piggy back along with the original script and use the approach that @Kent1Cooper  and @john.uhden  posted here.

 

 

 

(tblsearch "LAYER" "Chocolate")
(ssget "_X" '((8 . "Chocolate")))

 

 

If ssget yields nothing | tblsearch it true , you may need to search inside the blocks for the object.

Or perhaps, run purge layer. 

 


@colleen.dunn wrote:

Layer and height reasonably constant (I hope).


I hope so too.

In the posted code, this part is where you provide the correct layer name in place of "Chocolate" and the 2 statements "STATEMET 1" and "STATEMENT 2"

 

 

....
;; the layer name to search for
              "Chocolate"
;; Statement IF Layer is assigned to an object
     (list
       (strcat 
         "\\P{\\H0.38462x;" "STATEMENT 1"
  "}")
       
;; Statement IF Layer is NOT assigned to an object  
(strcat
 "\\P{\\H0.38462x;" "STATEMENT 2"
          "}")
   )
;; Target MTEXT
(setq data '("DEFAULT"  13.00))             
....               

 

 

HTH

Screencast

 

 
0 Likes
Message 16 of 20

pbejse
Mentor
Mentor

Moderator:

I must be doing something wrong that i somehow keep deleting my post  🙂

 


@colleen.dunn wrote:

Thank you so much.  This looks pretty close.  The property name is different for each drawing, and I don't know the name without opening each file.  So testing for the number, which I can supply and is different for each drawing, the layer (DEFAULT) and the text height (13.0) is the best choice.


I'm counting on you to say that , we can do away with this one.  It is settled then, we will use the layer and Mtext height.

 


@colleen.dunn wrote:

I have a number of routines to run on each drawing so I'm quite captivated by your suggestion that the routines can be run without graphically opening the files. 


I'm assuming you will runing a script [ scr ], we can piggy back on your existing script, but the way the code is writen , we dont have to.  It's nothing new, we do it all the time here.

 


@colleen.dunn wrote:

Sorry, I forgot to say that it is only a one-time thing. ..

...

Layer and height reasonably constant (I hope).


Yes, i figured as much. I hope so too 😄

 

This is the bit on the code that you need to provide the correct string values for Layer in place of "Chocolate" and the real statements in place of "STATEMENT 1" and  "STATEMENT 2"

 

....
;; the layer name to search for
              "Chocolate"
;; Statement IF Layer is assigned to an object
    (list
       (strcat 
         "\\P{\\H0.38462x;" "STATEMENT 1"
  "}")
;; Statement IF Layer is NOT assigned to an object
(strcat
 "\\P{\\H0.38462x;" "STATEMENT 2"
          "}")
   )
;; Target MTEXT
(setq data '("DEFAULT"  13.00))              
....               

 

 

I also provided a screencast [ hopefully this time it sticks ]

 

0 Likes
Message 17 of 20

pbejse
Mentor
Mentor

I suggest you make a copy of the files to be tested and dump it on a test folder. Try the routine on 5 to 10 drawings , the program will incldue subdirectory by default.

 

[  You do have a backup of all 9000 files of course, YES?  ] 👍

 

 

 

0 Likes
Message 18 of 20

pbejse
Mentor
Mentor

@pbejse wrote:

Moderator:

I must be doing something wrong that i somehow keep deleting my post  🙂

What is going on? Yesterday  the posts dissappeared, today its back, 😄

Now i'm doubling up on my posts

 

Anyhoo, i updated the ColleenDunn.lsp to check the target MTEXT if it already have a "STATEMENT". In this mod, the First and second line will remain and the current STATEMENT will be replcaced, I realize i need to do this if the user needs to update the file:

 

@Anonymous  we could even include a file selection, just in case you need to run this program on only a number of files. but then again, its a one time thing.

 

Refer to attached file for the updated program.

 

0 Likes
Message 19 of 20

Anonymous
Not applicable

Gosh PBJSE, for a little white dog with a funny face - you're a legend.  Thank you so much - I'm overwhelmed by your effort.  I'm just organising all of the routines, and will give it a little try and let you know.

 

(With my tongue firmly in my cheek) do you really think I need to give it a little try on a few files!??  Why not just go crazy and tackle the whole lot?  And only sad, obsessive weaklings backup!!  

 

Seriously, I've replicated the library remotely from the server in a minor way and will try until it surrenders, before going "live". 

 

Many, many thanks...Colleen

0 Likes
Message 20 of 20

pbejse
Mentor
Mentor

@Anonymous wrote:

Gosh PBJSE, for a little white dog with a funny face - you're a legend.  Thank you so much - I'm overwhelmed by your effort.  I'm just organising all of the routines, and will give it a little try and let you know.


You like that eh,  Dont thank me yet, until we can see that it actually works on your files. I'm curious about thos existing routines. mind telling us what it does?

 


@Anonymous wrote:

(With my tongue firmly in my cheek) do you really think I need to give it a little try on a few files!??  Why not just go crazy and tackle the whole lot? 


 I would advise against NOT doing a test on a couple files. We can never be too sure. Its your call Colleen. 

 


@Anonymous wrote:

... And only sad, obsessive weaklings backup!!  

 

Seriously, I've replicated the library remotely from the server in a minor way and will try until it surrenders, before going "live". 

 

Many, many thanks...Colleen


Youre not one of those I see 😄

Well good for you then. Tell me how it goes, and You are welcome Colleen

 

 

 

 

 

 

0 Likes