Replace circles with Dynamic blocks

Replace circles with Dynamic blocks

Anonymous
Not applicable
1,350 Views
15 Replies
Message 1 of 16

Replace circles with Dynamic blocks

Anonymous
Not applicable

Hi all,

 

I have bunch of circles with two different radiuses (see screenshot below):

 

1.jpg

 

They are all on "ASBUILT" layer.

 

I would like to grab them all by following SSGETs:

 

(setq ss-lv-poles (ssget "_x" '((-4 . "<or") (-4 . "<and") (0 . "circle") (8 . "ASBUILT") (40 . 0.75) (-4 . "and>") (-4 . "or>")) ) )

(setq ss-Hv-poles (ssget "_x" '((-4 . "<or") (-4 . "<and") (0 . "circle") (8 . "ASBUILT") (40 . 1.7) (-4 . "and>") (-4 . "or>")) ) )

And pass them to a function that I have (Thanks to Lee Mac's useful post) and replace them with a Dynamic block named "Power Pole". The smaller circle should have "Low Voltage Pole" as for its visibility state. Likewise, the double concentric circles should be replaced by same dynamic block with "Low Voltage, High Voltage Pole" visibility state.

 

Here is my code:

 

(defun PoleSymbolReplace  (ss1 ss2/ loopLength1 loopLength2 index1 index2 FlagVariable en1 en2 el1 el2 as1 as2)
	(if	(/= ss1 nil)
		(progn
			(setq loopLength1 (sslength ss1))
			(setq index1 0)
			(repeat	loopLength1
				(setq FlagVariable nil)
				(setq en1 (ssname ss1 index))
				(setq el1 (entget en1))
				(setq index1 (+ 1 index1))
				(progn
					(setq as1 (assoc 10 el1))
					(setq xyz (cdr as1))
					(if	(/= ss2 nil)
						(progn
							(setq loopLength2 (sslength ss2))
							(setq index2 0)
							(repeat	loopLength2
								(setq en2 (ssname ss2 index2))
								(setq el2 (entget en2))
								(setq index2 (+ 1 index2))
								(progn
									(setq as2 (assoc 10 el2))
									(if	(equal as1 as2 0.001)
										(progn 
											(LM:insertwithstate "Power Pole" "Low Voltage, High Voltage Pole" xyz)
											(entdel en2)
											(entdel en1)
											(setq FlagVariable T)
										)
									)
								)
							)
						)
					)
					(if	(not FlagVariable)
						(progn
							(LM:insertwithstate "Power Pole" "Low Voltage Pole" xyz)
							(entdel en1)
						)
					)
				)
			)
		)
	)
(princ)
)

 

But when I call it by:

 

(PoleSymbolReplace ss-lv-poles ss-Hv-poles)

I get  error: too few arguments

 

What am I doing wrong here?

0 Likes
Accepted solutions (1)
1,351 Views
15 Replies
Replies (15)
Message 2 of 16

cadffm
Consultant
Consultant

 

old   (defun PoleSymbolReplace  (ss1 ss2/  loopLength1 loopLength2 index1 index2 FlagVariable en1 en2 el1 el2 as1 as2)

want 13 argument

 

new (defun PoleSymbolReplace  (ss1 ss2 / loopLength1 loopLength2 index1 index2 FlagVariable en1 en2 el1 el2 as1 as2)

want 2 argument

 

 

Sebastian

0 Likes
Message 3 of 16

Anonymous
Not applicable

Thanks for the pick up but unfortunately that wasn't the issue 😞

 

I attached a sample drawing.

0 Likes
Message 4 of 16

cadffm
Consultant
Consultant

If that wasnt the issue, that is not your code 😉

But if you fixed that problem and there is a second issue with the same error:

Perhaps it is the unknown function 'LM:insertwithstate', we can not knowing how you defined it.

 

Sebastian

0 Likes
Message 5 of 16

Anonymous
Not applicable

That was the whole point. I mentioned a link to the Lee's post in my first post. The original code is asking user to select the insertion point. Whereas I tried to change it and pass the insertion coordinates hard-coded instead of asking from user.

 

(Lee's post is HERE again)

0 Likes
Message 6 of 16

Ranjit_Singh
Advisor
Advisor

As written

(defun PoleSymbolReplace  (ss1 ss2/ loopLength1 loopLength2 index1 index2 FlagVariable en1 en2 el1 el2 as1 as2)

you need 13 arguments. Add a space between ss2 and /

 

(defun PoleSymbolReplace  (ss1 ss2 / loopLength1 loopLength2 index1 index2 FlagVariable en1 en2 el1 el2 as1 as2)

This may get rid of the too few arguments error.

 

0 Likes
Message 7 of 16

Anonymous
Not applicable

Hey Ranjit,

 

Thanks for your reply. I already fixed that missing SPACE in DEFUN.

 

Program now throws different error:

 

; error: bad argument type: numberp: nil

 

No idea why !

 

------------

Apology to CADffm, I didn't notice the different error message after adding the SPACE character to DEFUN.

0 Likes
Message 8 of 16

Ranjit_Singh
Advisor
Advisor

Below works in my minimal testing. Make sure to load all functions from the post you mentioned and also this link

(defun PoleSymbolReplace  (ss1 ss2 / loopLength1 loopLength2 index1 index2 FlagVariable en1 en2 el1 el2 as1 as2)
	(if	(/= ss1 nil)
		(progn
			(setq loopLength1 (sslength ss1))
			(setq index1 0)
			(repeat	loopLength1
				(setq FlagVariable nil)
				(setq en1 (ssname ss1 index1))
				(setq el1 (entget en1))
				(setq index1 (+ 1 index1))
				(progn
					(setq as1 (assoc 10 el1))
					(setq xyz (cdr as1))
					(if	(/= ss2 nil)
						(progn
							(setq loopLength2 (sslength ss2))
							(setq index2 0)
							(repeat	loopLength2
								(setq en2 (ssname ss2 index2))
								(setq el2 (entget en2))
								(setq index2 (+ 1 index2))
								(progn
									(setq as2 (assoc 10 el2))
									(if	(equal as1 as2 0.001)
										(progn 
											(LM:insertwithstate "Power Pole" "Low Voltage, High Voltage Pole")
											(entdel en2)
											(entdel en1)
											(setq FlagVariable T)
										)
									)
								)
							)
						)
					)
					(if	(not FlagVariable)
						(progn
							(LM:insertwithstate "Power Pole" "Low Voltage Pole")
							(entdel en1)
						)
					)
				)
			)
		)
	)
(princ)
)
0 Likes
Message 9 of 16

Anonymous
Not applicable

Much appreciate it Ranjit.

 

No error this time but you removed the insertion point (XYZ) from this line:

 

 

(LM:insertwithstate "Power Pole" "Low Voltage, High Voltage Pole"  XYZ)

 

the XYZ variable read in line 13 was supposed to be passed into LM:insertwithstate and stop asking user to select block insertion point.

 

 

Am I missing something here? Did it work on your machine with the sample file I provided?

 

0 Likes
Message 10 of 16

Ranjit_Singh
Advisor
Advisor

Have you modified LM:insertwithstate function already to accept 3 arguments? If so, can you provide what you have so far?

0 Likes
Message 11 of 16

Anonymous
Not applicable

Thank you so much Ranjit.

 

I fixed the problem. Again another bloody SPACE was missing from modified version of Lee's code.

 

I paste it here in case if anyone else needs it for the same purpose.

 

All credit goes to Lee Mac (www.lee-mac.com)

 

 

Here is the code:

 

(defun LM:insertwithstate ( blk vis xyz / bse cmd def ent ext new obj pth rtn tmp )
    (setq pth (vl-string-translate "/" "\\" (vl-filename-directory blk)) 
          ext (cond ((vl-filename-extension blk)) (".dwg"))
          bse (vl-filename-base blk)
    )
    (if (/= "" pth)
        (setq pth (strcat pth "\\"))
    )
    (cond
        (   (not
                (or
                    (and
                        (tblsearch "block" bse)
                        (setq blk bse)
                    )
                    (setq blk (findfile (strcat pth bse ext)))
                )
            )
            (prompt (strcat "\nBlock \"" bse "\" not found."))
        )
        (   (progn
                (setq obj
                    (vlax-invoke
                        (vlax-get-property (LM:acdoc)
                            (if (= 1 (getvar 'cvport))
                                'paperspace
                                'modelspace
                            )
                        )
                        'insertblock
                        '(0.0 0.0 0.0)
                        blk
                        1.0 1.0 1.0 0.0
                    )
                )
                (vla-put-visible obj :vlax-false)
                (= :vlax-false (vla-get-isdynamicblock obj))
            )
            (vla-delete obj)
            (prompt (strcat "\nBlock \"" bse "\" is not dynamic."))
        )
        (   (null (LM:setvisibilitystate obj vis))
            (vla-delete obj)
            (prompt
                (strcat
                    "\nUnable to set visibility state of block \""
                    bse
                    "\" to \""
                    vis
                    "\"."
                )
            )
        )
        (   (setq tmp 0)
            (while
                (tblsearch "block"
                    (setq blk
                        (strcat "tmp" (itoa (setq tmp (1+ tmp))))
                    )
                )
            )
            (vla-put-visible
                (car
                    (vlax-invoke (LM:acdoc) 'copyobjects (list obj)
                        (setq def
                            (vlax-invoke
                                (vla-get-blocks (LM:acdoc))
                                'add
                                '(0.0 0.0 0.0)
                                blk
                            )
                        )
                    )
                )
                :vlax-true
            )
            (vla-delete obj)
            (setq ent (entlast)
                  cmd (getvar 'cmdecho)
            )
            (setvar 'cmdecho 0)
           
            (if
                (and
                    (vl-cmdf "_.-insert" blk "_S" 1.0 "_R" 0.0 xyz)
                    (not (eq ent (setq ent (entlast))))
                    (setq new (vlax-ename->vla-object ent))
                    (= "AcDbBlockReference" (vla-get-objectname new))
                )
                (progn
                    (setq rtn (car (vlax-invoke new 'explode)))
                    (vla-delete new)
                )
            )
            (setvar 'cmdecho cmd)
            (vl-catch-all-apply 'vla-delete (list def))
        )
    )
    rtn
)
0 Likes
Message 12 of 16

stevor
Collaborator
Collaborator

1. Looks like LM:insertwithstate should have 2 args, not 3.

2. WHy use dynamic blocks?

 

S
0 Likes
Message 13 of 16

Anonymous
Not applicable
You are right. That's why I added the third argument to Lee's code.
Dynamic block is what is accepted in the project that I am working on (unfortunately !)
0 Likes
Message 14 of 16

Ranjit_Singh
Advisor
Advisor
Accepted solution

This seems to work. I am not sure if the results are as expected. You need to verify.

(defun polesymbolreplace  (ss1 ss2 / looplength1 looplength2 index1 index2 flagvariable en1 en2 el1 el2 as1 as2)
  (if (/= ss1 nil)
    (progn (setq looplength1 (sslength ss1))
           (setq index1 0)
           (repeat looplength1
             (setq flagvariable nil)
             (setq en1 (ssname ss1 index1))
             (setq el1 (entget en1))
             (setq index1 (+ 1 index1))
             (progn (setq as1 (assoc 10 el1))
                    (setq xyz (cdr as1))
                    (if (/= ss2 nil)
                      (progn (setq looplength2 (sslength ss2))
                             (setq index2 0)
                             (repeat looplength2
                               (setq en2 (ssname ss2 index2))
                               (setq el2 (entget en2))
                               (setq index2 (+ 1 index2))
                               (progn (setq as2 (assoc 10 el2))
                                      (if (equal as1 as2 0.001)
                                        (progn (lm:insertwithstate "Power Pole" "Low Voltage, High Voltage Pole" xyz)
                                               (entdel en2)
                                               (entdel en1)
                                               (setq flagvariable t)))))))
                    (if (not flagvariable)
                      (progn (lm:insertwithstate "Power Pole" "Low Voltage Pole" xyz) (entdel en1)))))))
  (princ))

replace_circ_w_dyn.gif

 

Message 15 of 16

braudpat
Mentor
Mentor

 

Hello Ranjit (MAPCAR Jedi Master)

 

Beautiful Routine and (French Humour) you haven't use MAPCAR ---> Bravo !

 

Regards, Patrice

 

 

Patrice ( Supporting Troops ) - Autodesk Expert Elite
If you are happy with my answer please mark "Accept as Solution" and if very happy please give me a Kudos (Felicitations) - Thanks

Patrice BRAUD

EESignature


0 Likes
Message 16 of 16

Ranjit_Singh
Advisor
Advisor

@braudpat since mapcar isn't there you can conclude that it isn't my routineSmiley LOL It actually belongs to @Anonymous

0 Likes