Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Replacing substr(s) in a string variable

14 REPLIES 14
SOLVED
Reply
Message 1 of 15
aqdam1978
3806 Views, 14 Replies

Replacing substr(s) in a string variable

Hi,

I write a code to replace strings in one string.
for example
str="AutoDesk, AutoCAD, 2013, Electrical, 64Bit"

patt=", "

NewStr=" - "

==> result: "AutoDesk - AutoCAD - 2013 - Electrical - 64Bit"

 

 

;;replaces NewStr instead of Patt(s) in Str string
(defun ReplaceStr (NewStr Patt Str)
(While (> (vl-string-search ptt str) 0)
	(setq Str (vl-string-subst NewStr patt Str)) 
);;while
str
)
  
; usage
;(ReplaceStr " - " ", " "AutoDesk, AutoCAD, 2013, Electrical, 64Bit")

  

==> result= ; error: bad argument type: (or stringp symbolp): nil

 

What is my mistake(s)?

 

Thanks

 

 

14 REPLIES 14
Message 2 of 15
Rtogores
in reply to: aqdam1978

A similar function that works is included in Chapter 5 (Listing 5.12) of my book on AutoLISP/Visual LISP.

This is the code:

(defun replace (new old string)
  (while (vl-string-search old string)
    (setq string (vl-string-subst new old string))
  ) ;_ end of  while
) ;_ end of  defun
;;;Listing 5.12. Replacement of characters in a string.

But, if you only want to replace the "," with a  "-" without including a space before the dash you can use:

_$ (vl-string-translate "," "-" "AutoDesk, AutoCAD, 2013, Electrical, 64Bit")
"AutoDesk- AutoCAD- 2013- Electrical- 64Bit"
_$ 

 

More info in my blog lispexpert.blogspot.com.

Message 3 of 15
aqdam1978
in reply to: Rtogores

Thank you for your answer, But what was my mistake? why my lisp did not work?

 

Thanks,

 

 

Message 4 of 15
hmsilva
in reply to: aqdam1978

aqdam,

your code works, just miss an "a" in the second "patt"

 

;;replaces NewStr instead of Patt(s) in Str string
(defun ReplaceStr (NewStr Patt Str)
(While (> (vl-string-search patt str) 0)
	(setq Str (vl-string-subst NewStr patt Str)) 
);;while
str
)
  
; usage
;(ReplaceStr " - " ", " "AutoDesk, AutoCAD, 2013, Electrical, 64Bit")

 

 

Henrique

EESignature

Message 5 of 15
Rtogores
in reply to: aqdam1978

Yes, only a typo, ptt for patt.

But the point is that the function can be simplified, as vl-string-search will return nil when no further ocurrences are found, and the value returned by the last setq will be the value returned by the function.

Message 6 of 15
aqdam1978
in reply to: Rtogores

aaaah, yes,

you are right! also, I can simplify it as you said:

 

(defun ReplaceStr (NewStr Patt Str)
	(While (vl-string-search patt str)
		(setq Str (vl-string-subst NewStr patt Str)) 
	);;while
)

 Thanks

Message 7 of 15
aqdam1978
in reply to: hmsilva

Hi,

I have a problem with this code:

(defun ReplaceStr (NewStr Patt Str)
	(While (vl-string-search patt str)
	(setq Str (vl-string-subst NewStr patt Str)) 
	);;while
str ;;in case of no patt exist in str, just return str itself
)

 as you know in prompt command we have:

(prompt "c:\\") (princ) ==> c:\
(prompt "c:\\\\")(princ) ==> c:\\

 

so, why this code does not work and AutoCAD goes to "no response" situation?

(prompt (ReplaceStr "\\\\" "\\" "c:\\"))(princ)  ;;AutoCAD will be freeze!

 

Thanks,

 

Message 8 of 15
pbejse
in reply to: aqdam1978


@aqdam1978 wrote:

Hi,

 

(prompt (ReplaceStr "\\\\" "\\" "c:\\"))(princ)  ;;AutoCAD will be freeze!

 

Thanks,

 


Because of an endless loop aqdam1978  (vl-string-search patt str) will never evaluate to nil 

 

Try this instead  using vl-string-subst to do a find and replace in external file by AlessiMarc'Antonio

 

HTH

Message 9 of 15
Rtogores
in reply to: aqdam1978

so, why this code does not work and AutoCAD goes to "no response" situation?

(prompt (ReplaceStr "\\\\" "\\" "c:\\"))(princ)  ;;AutoCAD will be freeze!

In this case you are just adding the same character to the string, not replacing it with a different one, so vl-string-search will always find that character and add a couple of new occurrences in each loop, so it'll never conclude.
To catch these kind of error I would suggest you set a breakpoint in your code and open a watch window to inspect the *LAST-VALUE*. Then call the function in the VLISP Editor and use the Step Into button to evaluate each expression. For a video explaining this way to inspect a program's workings, you can see Module 5 of my AU class: http://au.autodesk.com/?nd=class&session_id=10671.
In this case you do not need your replacement function.(vl-string-subst "\\\\" "\\" "C:\\") will do.




Message 10 of 15
Rtogores
in reply to: Rtogores


Rtogores wrote: For a video explaining this way to inspect a program's workings, you can see Module 5 of my AU class: http://au.autodesk.com/?nd=class&session_id=10671.

Also (better) in Module 3!


 

Message 11 of 15
aqdam1978
in reply to: Rtogores

yes, you are right!

 

I changed my code and now it support any conditions:

 

;;replaces NewStr instead of Patt(s) in Str string
(defun ReplaceStr (NewStr Patt Str / pos)
	(setq pos 0)
	(While (setq pos (vl-string-search patt str pos))
		(setq Str (vl-string-subst NewStr patt Str pos))
		(setq pos (+ pos (strlen NewStr)))
	)
str
)
;;usage
;;(ReplaceStr "AAA" "A" "A-A-A")

 there is no any exceptional situation to freeze autocad!

 

Thanks,

 

Abbas

 

Message 12 of 15
Rtogores
in reply to: aqdam1978

Great Abbas!

It can still be streamlined a bit, no need for the initial (setq pos 0) as being pos = nil it will use the default position 0.


;;replaces NewStr instead of Patt(s) in Str string
(defun ReplaceStr (NewStr Patt Str / pos)
;;;	(setq pos 0) this can be omitted
	(While (setq pos (vl-string-search patt str pos))
		(setq Str (vl-string-subst NewStr patt Str pos))
		(setq pos (+ pos (strlen NewStr)))
	)
str
)
;;usage
;;(ReplaceStr "AAA" "A" "A-A-A")
Reinaldo

 




Message 13 of 15
mdhutchinson
in reply to: Rtogores

Lisp your mesh into shape?

 

Is it possible to concert poly face meshes into 3dsoldids via lisp??

Message 14 of 15
Rtogores
in reply to: mdhutchinson

You could make a lisp program that reads the PFACE vertices and use the data to entmake a new MESH object like the ones shown in the video. This can be done using the programs included in the class handout and dataset. But this can be also done using commands:

You should convert the Polyface Mesh into a new MESH object before. It must be a "watertight" MESH, that means it should enclose a volume, no holes in it. To do this use the _MESHOPTIONS  command selecting the appropriate options. For example see the attached image. You must select the Polyface mesh using the button on the upper left corner, of the dialog, select the appropriate mesh type. In the case shown in the image, as the faces are triangular, I'll select Triangle. For maximum angle between new faces I'll select 180 degrees so no new faces are added. I cam prewiew the resulting mesh using the Preview button. If the resulting mesh is what I am looking for, ENTER will make the transformation.

Once the PFACE mesh is converted to a new MESH object I can use the _CONVTOSOLID command to create the new 3DSolid entity. If the mesh is not watertight you can try the _THICKEN command that will create a solid slab with the specified thickness. You can see examples of this in my videos.

 

Message 15 of 15
Rtogores
in reply to: Rtogores

Video on converting Polyface Meshes into 3DSolids:

http://www.youtube.com/watch?v=jH0xVgWqBZ0

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost