How to find multiple objects and replace them with different objects?

How to find multiple objects and replace them with different objects?

Anonymous
Not applicable
28,282 Views
64 Replies
Message 1 of 65

How to find multiple objects and replace them with different objects?

Anonymous
Not applicable

How to find multiple objects and replace them with different objects? I am trying to have AutoCAD 2017 find for example all the red filled circles in the drawing and replace them with another drawing. Instead of manually doing it one by one. I saw one youtube video about it, but I didn't have the Cpp tab. Nor understand how to get it. As you can see this lady use in her example.

 

https://www.youtube.com/watch?v=rpgT5xy-Syc&t=36s

0 Likes
Accepted solutions (2)
28,283 Views
64 Replies
Replies (64)
Message 2 of 65

SeeMSixty7
Advisor
Advisor

I can't see the demonstration very well on my phone, but it looks like she is showing you how to use a custom command that either she provides or her company as a utility software solution.

 

It looks like her solution is working with block and just replacing the contents of those blocks with a different block. Similar o your goal, but a little different.

 

You can write a lisp routine to step through all the circles and then insert your block at the center of the circle. I assume Rotation would be zero.

 

This is just a quick sample. It does not look for Red Circles, because I did not know if they are actually red or on a layer that is red in color. You also stated filled, did you mean with a hatch?

 

This one just looks for circles. You can get more specific with it if you play with it.

(defun c:quick()
	(setq myss (ssget "X" '((0 . "CIRCLE"))))
	(if myss
		(progn
			(setq mylen (sslength myss)
				  mycnt 0
			)
			(while (< mycnt mylen)
				(setq myent (ssname myss mycnt)
					  myins (cdr (assoc 10 (entget myent)))
					  mycnt (1+ mycnt)
				)
				(command "-insert" "[YOUR BLOCKNAME HERE]" myins "" "" "")
			)
		)
	)
	(princ)
)

 You might want to post in the "Visual LISP, AutoLISP and General Customization" area though to get help on this. 

0 Likes
Message 3 of 65

RobDraw
Mentor
Mentor

@Anonymous wrote:

I didn't have the Cpp tab. Nor understand how to get it.


It is a customization that is available here: http://www.cpprs.com/.


Rob

Drafting is a breeze and Revit doesn't always work the way you think it should.
0 Likes
Message 4 of 65

Vinayv4v
Advisor
Advisor

Hi,

 

Its a plugin and can be purchased from this site for a 15 day free trial or for 1 year paid version

 

http://www.cpprs.com/store.html

plugin.JPG

 

If it was a single block then you could have replaced it with the command BLOCKREPLACE from express tools.

Cheers,

Vinay Vijayakumaran

0 Likes
Message 5 of 65

Anonymous
Not applicable

I am unfamiliar with coding in AutoCAD. Could you please explain a little bit on how to get it to operate in the drawing. I found the visual LISP editor and posted the code in their but it says nil every time it runs. I do mean hatch where ever I said filled. A the same time,  when I replace those objects with a different block, it does not replace them at every spot the objects were at. It is because it is viewed as one object, rather than all separate. So how do I get it to replace all of the same objects with all of a different object. can this be done without coding? Thank you

0 Likes
Message 6 of 65

SeeMSixty7
Advisor
Advisor

If you are referring to the code that I posted, you can simply copy and paste the text into a blank notepad document, then save as "SOMETHING.LSP" ina  folder located on the AutoCAD Path, Your document folder should work ok. Be sure change your block name where it calls for it in the code.

 

"[YOUR BLOCKNAME HERE]"

 

Change [YOUR BLOCKNAME HERE] to be your blocks name.

 

Then you can drag and drop the file into your AutoCAD drawing area. It will then load the new command for you.

 

Then you just run the command by typing QUICK at the AutoCAD command prompt.

 

Good luck,

0 Likes
Message 7 of 65

Anonymous
Not applicable

I ran your code and I got it to work. What I have issues is going from the file, named after import, to the file, named what I want to show. the first .dwg file shows really weird symbols that resulted from an importing of a pdf into AutoCAD. the second .dwg file I had to do by hand. this is one portion of hundreds I need to do. How would you adapt your code to select those weird objects? It would not be a circle. But how do you figure out what the software calls them to replace the world CIRCLE in your code?

 

(defun c:quick()
(setq myss (ssget "X" '((0 . "CIRCLE"))))
(if myss
(progn
(setq mylen (sslength myss)
  mycnt 0
)
(while (< mycnt mylen)
(setq myent (ssname myss mycnt)
  myins (cdr (assoc 10 (entget myent)))
  mycnt (1+ mycnt)
)
(command "-insert" "[YOUR BLOCKNAME HERE]" myins "" "" "")
)
)
)
(princ)
)

0 Likes
Message 8 of 65

SeeMSixty7
Advisor
Advisor

You would want to create a conditional replace system that is based on the MTEXT objects with the text value being the controlling element.

 

You also have some different layer elements that would be controlled by the original color. Consider adding more code for that.

 

This code will use blocks to insert based on the insertion points of the mtext entities.

Things to consider. This will not erase the old entities, you will want to do that when you get everything all done.

You will want to create blocks to meet the desired outcome. You can do it differently, but blocks will be probably the easiest solution.

This will not do anything with the hatch patterns, you can add code in for that if you wish too.

The insertion point may not come out the way you want since the mtext insertion point could prove to be erratic. You may want to use an osnap arodn that point to get what you want.

 

Good luck,

(defun c:quick();defines the new command
	(setq myss (ssget "X" '((0 . "MTEXT")))) ; creates a selection set of MTEXT entities
	(if myss ; verifies there is a selections set to process
		(progn ; groups together processes
			(setq mylen (sslength myss) ; how many mtext ents
			      mycnt 0 ; starting point counter
			)
			(while (< mycnt mylen) ; whiel loop to step through all ents
				(setq myent (ssname myss mycnt) ; pick the index item out of the selection set using cnt variable
					  myentdata (entget myent); get the ents dxf data
					  myins (cdr (assoc 10 myentdata)) ; get the insertion point (dxf code 10)
					  mymtextval (cdr (assoc 1 myentdata)); get the text value in the ent
				      mycnt (1+ mycnt) ; bump our counter for the next ent on next iteration
				      blname "Unknown" ; set the blname variable to unknown to do nothing with.
				)
				(cond ; start conditionl block
					((= mymtextval "(!") ; is the mtext value this?
						(setq blname "switch") ; then set my blockname to this
					)
					((= mymtextval ")\"") ; Same as above
						(setq blname "recloser")
					)
					((= mymtextval "kà")
						(setq blname "regulator")
					)
					;;add more options here
				)
				(if (/= blname "Unknown") ; if there is a blockname found
					(command "-insert" blname myins "" "" "") ; insert the block
				)
			)
		)
	)
	(princ)
)
0 Likes
Message 9 of 65

RobDraw
Mentor
Mentor

@Anonymous wrote:

I didn't have the Cpp tab. Nor understand how to get it.

 

https://www.youtube.com/watch?v=rpgT5xy-Syc&t=36s


So, have you tried the Cpp program? It was what you were looking for but you seem to have given up on it. I'd be interested to here how your trial went.


Rob

Drafting is a breeze and Revit doesn't always work the way you think it should.
0 Likes
Message 10 of 65

Anonymous
Not applicable

the does not work for the cpp. When I put it in google, it won't let me access the webpage. It says it is down. Is that the case for you? If not, then security blocks it.

0 Likes
Message 11 of 65

Anonymous
Not applicable

I tried your code in the same .dwg file I uploaded to the forum and it did not work. It starts to look for a file a named regulator. It does not ask me to do anything, it just cancels. I have no option of selecting the block I want it to replace it with.

0 Likes
Message 12 of 65

SeeMSixty7
Advisor
Advisor

I used the block names you had in your sample drawing file. The finished product one. Do you have those block available on the acad path? If not WBLOCK them from the drawing you had that had the completed method in to the same folder your current drawing is located in or a folder on the acad path environment variable.

 

Good luck,

0 Likes
Message 13 of 65

Anonymous
Not applicable

I am not sure what you mean. Both file are saved on my desktop. Can you explain what you mean a little more if that is not what you were talking about them being saved in the same spot. Thank you.

0 Likes
Message 14 of 65

SeeMSixty7
Advisor
Advisor

The block Definitions need to be in the file you are running the Lisp routine on.

 

A quick Cheat would be to Copy everything from your finished drawing into your test drawing, then erase what you copied in. Then the block definitions will be in the drawing.

 

Good luck,

 

 

 

 

0 Likes
Message 15 of 65

RobDraw
Mentor
Mentor

I think he means that the code is looking for the .dwgs of the blocks as separate files and the file paths need to be defined as a support path in AutoCAD. They could also be in the same folder as the file you are running the code from.

 

I'm not sure as I'm not fluent in LISP by any stretch of the imagination.


Rob

Drafting is a breeze and Revit doesn't always work the way you think it should.
Message 16 of 65

Anonymous
Not applicable

Once I copied the blocks over. I reran the code and got an error, which is attacehed. I still never typed anything, it searched on its own for a file named switch.dwg, but why would it be looking for another drawing instead of looking for an object inside the drawing

0 Likes
Message 17 of 65

SeeMSixty7
Advisor
Advisor

Ok I went back and looked at what I sent you. It is looking for a block to insert. It is not looking to open another drawing. I assumed you had created the blocks I talked about in the post with the code. Attached is a sample drawing you can test on. I included some sample blocks in it based on your final result drawing.

 

This is only a sample of how to get started. You will need to compensate for the Adjusted insertion point. You could use an osnap or calculate based on the box around the text you are replacing. Keep in mind it does not erase the old text either. The routine is a starting point for you to get going.

 

Good luck,

 

 

0 Likes
Message 18 of 65

Anonymous
Not applicable

All I did to create the blocks, was I copied the block the whole drawing and pasted it into the one I was working on with the weird symbols. Is that how you get it to look for the right symbols.

 

 

0 Likes
Message 19 of 65

SeeMSixty7
Advisor
Advisor

You really only need to copy an instance of each block into your drawing. You can also WBLOCK those to a path located on the AutoCAD ACAD Path variable.

 

I only setup the case for 3 types. I put a place in the code for you to add more cases. you can create your own blocks and add your own conditions to meet for each type.

 

It does not seem like you have been using AutoCAD very long, is that right? You are jumping into Customizing it pretty quick. That's one way to learn, grin. Keep going you will get there.

 

Good Luck,

0 Likes
Message 20 of 65

Anonymous
Not applicable

When I tried adding to the code you produced, it went through a couple of the swtiches and reported an error. If there was an error from the stuff I added shouldn't it have gone through the first three scenarios. Also How can I get it to replace the S" in the script because I tried that but left it as an S because it thought I was using coding notation and messed up the rest of the code after that in the script.

 

Attached is the drawing

 

 

 

(defun c:quicks();defines the new command
 (setq myss (ssget "X" '((0 . "MTEXT")))) ; creates a selection set of MTEXT entities
 (if myss ; verifies there is a selections set to process
  (progn ; groups together processes
   (setq mylen (sslength myss) ; how many mtext ents
         mycnt 0 ; starting point counter
   )
   (while (< mycnt mylen) ; whiel loop to step through all ents
    (setq myent (ssname myss mycnt) ; pick the index item out of the selection set using cnt variable
       myentdata (entget myent); get the ents dxf data
       myins (cdr (assoc 10 myentdata)) ; get the insertion point (dxf code 10)
       mymtextval (cdr (assoc 1 myentdata)); get the text value in the ent
          mycnt (1+ mycnt) ; bump our counter for the next ent on next iteration
          blname "Unknown" ; set the blname variable to unknown to do nothing with.
    )
    (cond ; start conditionl block
     ((= mymtextval "(!") ; is the mtext value this?
      (setq blname "switch") ; then set my blockname to this
     )
     ((= mymtextval ")\"") ; Same as above
      (setq blname "recloser")
     )
     ((= mymtextval "kà")
      (setq blname "regulator")
     )
     ((= mymtextval ",%")
      (setq blname "sectionalizer")
     )
     ((= mymtextval "S")
      (setq blname "substation")
     )
     ((= mymtextval "k")
      (setq blname "fuse")
     )
     ;;add more options here
    )
    (if (/= blname "Unknown") ; if there is a blockname found
     (command "-insert" blname myins "" "" "") ; insert the block
    )
   )
  )
 )
 (princ)
)

0 Likes