Jump to content

Convert Corrupted Legends into Block or on Legend in AutoCAD


Recommended Posts

I have a high-resolution PDF copy of an electrical tender project drawing made with AutoCAD. I converted the PDF to DWG, but the legends were distorted because of the conversion process.

The legends are composed of multiple lines that look like drawings. Is there any way to select one legend and tell AutoCAD to find all similar legends and make each one of them a separate block or Legend that I can count and select?



Note: I tried the Select Similar command and selected all similar objects, and I could find the similar legends, but the count was still based on the lines and not the legends."

Link to comment
Share on other sites

I apologize, but I lack the authorization to distribute the project. However, I can provide a screenshot of the Floor Trap legend where you'll be able to observe the count (930), though there are approximately 73 unique ones. Additionally, if you're interested, I can furnish the legends in a separate DWG file.


Link to comment
Share on other sites

Are all these details on their own layer like your screen shot would suggest? The problem I have ever had is selecting similar groups from within a whole drawing but here if all the blue coloured details are the same (to be made into 1 block) and all the red are another and can be separated from the other details then the selection process can be a lot simpler.


I'd go:

Manually turn on / off the layers

LISP: Select everything that is blue and a polyline (use ssget, guessing here that the rectangle is the only polyline)

Count can be the size of the selection set


Yes, if you can post (as a dwg) the legends detail as it comes from the PDF, and perhaps a snip, a small portion of the drawing for testing purposes - remove all project data, names and so on (ie copy to a blank drawing and send that)




Link to comment
Share on other sites

I've included a segment from the drawing and the primary PDF for testing purposes.

The method I've found to single out similar objects is by first applying the "Overkill" command. After that, I use the "SELECTSIMILAR" command and choose all the available options within the "SELECTSIMILAR" settings.


DRAINAGE_10.pdf Drawing1.dwg

Link to comment
Share on other sites

So this is quite a basic counter:


It will ask you to select an object in the group and then select the drawing area.


The first selection sets the search criteria (ssget with a filter) - here searching for anything of the same entity type, on the same layer with the same colour (can add more filters later if needed).


So if you select a circle on layer "PDF_Waste_Pipe" with a colour ByLayer it will return how many of these it finds... even if there are 2 or more in that group.


Obviously it will work the best if your legend / blocks are on a unique layer - which in your sample they look like they are - and you pick an entity that only exists once in each group like a circle or rectangle.


However looking quickly at your drawing I think this might cover what you are doing I think.


I'll have a think to see if there is a more elegant way, I suspect that there is using the ssget function, crossing polylines and removing items from selection sets


(defun c:sel ( / MyEnt EntLay EntType EntCol EntClosed MySS)
  (setq MyEnt (car (entsel "Select an Entity to count: ")))
  (if MyEnt
      (princ "Thanks. Now select the drawing area to count")
      (setq EntLay (assoc 8 (entget MyEnt)))
      (setq EntType (assoc 0 (entget MyEnt)))
      (setq EntClosed (assoc 70 (entget MyEnt)))
      (setq EntCol (assoc 62 (entget MyEnt)))

      (setq SSList (list EntType EntLay ))
      (if (= EntClosed nil) () (setq SSList (cons EntClosed SSList )))
      (if (= EntCol nil) (setq SSList (cons (cons 62 256) SSList ))) ; colour by layer

      (setq MySS (ssget SSList ))
    ) ; end progn
    (princ "Nothing selected")
  ) ; end if

  (princ (sslength MySS))(princ " blocks found")


  • Like 1
Link to comment
Share on other sites

It functions effectively alongside the Floor Trap Legend. However, in the attached screenshot, there's a similar legend. When I choose the triangle, it counts both the single and twin switches. Is it possible to incorporate a feature that allows for multiple object selection or filtering by line length, perhaps? As seen in the second image, you can observe the distinctions between the legends.


Screenshot 2023-10-28 112805.jpg

Link to comment
Share on other sites

From what I made up above that will be a bit tricky though possible.


I wonder, how much separation is there between the blocks in the drawing? Mostly, do any of them overlap?


Like I mentioned above the selection works on a 'Selection set' which for a line is limited to quite a basic filter, start and end coordinates, layer, colour, line type. Other things such as line length can be inferred from the coordinates but you can't filter that.


I've been to sleep since last week... and can't quite remember your sample drawing exactly. I am thinking that perhaps the selection set could be done in 2 parts. 

Something like this:

Select an object in one of your legend blocks. This will let the LISP work out the entity type (circle etc), layer, colour for all the other similar symbols

The user then selects a 'bounding box' enclosing all the symbols entities and the LISP can work out coordinates of that.

Next stage is to select all the entities in that area (based on the criteria recorded from the first entity selected - layers etc), do a count of them

Then... going back to what I did above, find all the similar entities in the drawing to the selection. These might be for example the 2 or 3 switch types also.... 

Now loop through this selection and apply the bounding box coordinates, modified to account for rotation and scaling to select all the object in an area around that entity (ssget with 'crossing polygon' filter)

Finally with this selection you might be able to count all the entities in there, their type, calculate the lengths of lines and confirm that this is indeed a symbol that you want to count. There count, 1.

And repeat....



The would rely however that the layer with the symbol on is reasonably uncluttered (if it was cluttered this might take a long time)



So it is possible but does need a fair bit of fiddling about to make it work ! Could be a dead useful LISP to have, perhaps taking it further to replace the individual symbol lines with a block.


Sadly not something I'll have time for this week, but with luck someone else might be able to help out and do something too

  • Like 1
Link to comment
Share on other sites

Just updating what I have above to be a bit more specific - not sure if it will be any different just now though - it should do the same as before. Ignore the rectangle part, it does nothing yet, just thinking ahead to the next steps


(defun c:selsimilar ( / MyEnt MySSList)

  (setq MyEntity (car (entsel "\nSelect an entity in the symbol: ")))
  (setq pt1 (getpoint "Select Symbol Objects"))
  (command "Rectangle" Pt1 pause) ;;come back to this line when I remember variable for last mouse clicked point
  (entdel (entlast))

  (if (= MyEntity nil)
    (princ "\nNothing Selected")
      (setq MyEnt (entget MyEntity))
;;Get entity definition to identify objects:
;;Basic Entity Definitions - Line + all below
      (setq MyEnt0  (assoc 0  MyEnt))
      (setq MyEnt67 (assoc 67 MyEnt))
      (setq MyEnt8  (assoc 8  MyEnt))
      (setq MyEnt62 (assoc 62 MyEnt))
      (setq MyEnt90 (assoc 90 MyEnt))
      (setq MyEnt70 (assoc 70 MyEnt)) ;;+ hatch + block
      (setq MyEnt43 (assoc 43 MyEnt))
      (setq MyEnt38 (assoc 38 MyEnt))
      (setq MyEnt39 (assoc 39 MyEnt))
;;Circle, ellipse
      (setq MyEnt40 (assoc 40 MyEnt))
;;Ellipse, block
      (setq MyEnt41 (assoc 41 MyEnt))
      (setq MyEnt42 (assoc 42 MyEnt))
      (setq MyEnt50 (assoc 50 MyEnt))
      (setq MyEnt51 (assoc 51 MyEnt))
;;Hatch, block
      (setq MyEnt2  (assoc 2  MyEnt))
      (setq MyEnt71 (assoc 71 MyEnt))
;;Hatch only
      (setq MyEnt91 (assoc 91 MyEnt))
      (setq MyEnt92 (assoc 92 MyEnt))
      (setq MyEnt93 (assoc 93 MyEnt))

;;Create a list to select identical entities
      (setq MySSList (list MyEnt0 MyEnt67 MyEnt8))

      (if (= MyEnt2 nil)()(setq MySSList (cons MyEnt2 MySSList )) )
      (if (= MyEnt38 nil)()(setq MySSList (cons MyEnt38 MySSList )) )
      (if (= MyEnt39 nil)()(setq MySSList (cons MyEnt39 MySSList )) )
      (if (= MyEnt40 nil)()(setq MySSList (cons MyEnt40 MySSList )) )
      (if (= MyEnt41 nil)()(setq MySSList (cons MyEnt41 MySSList )) )
      (if (= MyEnt42 nil)()(setq MySSList (cons MyEnt42 MySSList )) )
      (if (= MyEnt43 nil)()(setq MySSList (cons MyEnt43 MySSList )) )
      (if (= MyEnt50 nil)()(setq MySSList (cons MyEnt50 MySSList )) )
      (if (= MyEnt51 nil)()(setq MySSList (cons MyEnt51 MySSList )) )
      (if (= MyEnt62 nil) (setq MySSList (cons (cons 62 256) MySSList )) (setq MySSList (cons MyEnt62 MySSList )) )
      (if (= MyEnt70 nil)()(setq MySSList (cons MyEnt70 MySSList )) )
      (if (= MyEnt71 nil)()(setq MySSList (cons MyEnt71 MySSList )) )
      (if (= MyEnt90 nil)()(setq MySSList (cons MyEnt90 MySSList )) )
      (if (= MyEnt91 nil)()(setq MySSList (cons MyEnt91 MySSList )) )
      (if (= MyEnt92 nil)()(setq MySSList (cons MyEnt92 MySSList )) )
      (if (= MyEnt93 nil)()(setq MySSList (cons MyEnt93 MySSList )) )

      (princ "\Thanks. Analysed sample entity. Now to select the rest. ")
      (setq MySS (ssget MySSList))

    ) ; end prgn
  ) ; end if

  (princ "\n")(princ (sslength MySS))(princ " similar entities")



  • Like 2
Link to comment
Share on other sites

Thank you for your detailed answer. I appreciate your effort and time to help me with this problem. You have given me some useful ideas on how to approach the task.

However, I still have some issues with the selection set. Sometimes, some lines from outside the blocks are selected and counted, which messes up the result. I think this is because the lines are close to the blocks or have similar properties. Is there a way to avoid this?

I have attached an electrical drawing for testing purposes. You can see that some of the blocks have extra lines around them that are not part of the symbol. Can you please take a look and see if you can modify your code to exclude them?

Thank you again for your help and patience. I hope to hear from you soon.


Link to comment
Share on other sites

Didn't get time to look at this last night, the method I was using was very quick but is going to give errors if there are similar sized entities on the same layer that are not a part of the block.


The next part is to take the entities it counts above and check that they are a part of the blocks... which might take some thinking and testing

  • Thanks 1
Link to comment
Share on other sites

Struggling to make this one work well enough, still thinking about it though - could be useful for other things, so one I might keep plugging away at as and when work allows (Here I am thinking also some sort of conversion from 'exploded' texts imported from PDFs to real texts... if it can work well enough)

  • Like 1
Link to comment
Share on other sites

Thank you for your comment. I appreciate your feedback. I’m glad to hear that you find it useful for other things.

That sounds like a very interesting and challenging task. I hope you can make it work well enough, and I look forward to seeing your progress.

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.

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...