Manipulating Text Strings in ANSYS Mechanical APDL

By: Eric Miller
– December 7, 2011
Categories:

The world is all gates, all opportunities, strings of tension waiting to be struck. – Ralph Waldo Emerson

The ANSYS Parametric Design Language has a lot of features in it.  Over the years it has grown to meet the needs of a growing population of users and has gone from a simple command scrip to a fairly sophisticated programming tool.  One area of the tool that power users find significant value is are the functions that are used to manipulate text strings.  You can use these functions to do all sorts of things that are needed to automate a process or produce more informative results.  Some examples are:

  • Gather information about your model and create a screen for display on a plot or in a report
  • Get information about files and directories and use that to manage, create, delete, etc…, those files.
  • Build commands up so APDL can write APDL or input to other programs
  • Create or modify parameter names for use in your macros

In this article we will look at strings in APDL and how to work with them.  You can find additional, more detailed information in the ANSYS Help, Mechanical APDL>ANSYS Parametric Deign Language Guide.  There is also more information on this topic and all things APDL in PADT’s Guide to the ANSYS Parametric Design Language (APDL).

Strings in ANSYS Mechanical APDL

Pull the string, and it will follow wherever you wish. Push it, and it will go nowhere at all. – Dwight D Eisenhower

Before we talk about manipulating strings, we should do some background on strings.

Strings can either be stored as a character parameter or as a string array. Character parameters are nice because they don’t require indices when you use them, and you don’t have to define them with a *dim. The down side is that you are limited to 8 characters.

You create a string parameter by using the parameter = method:

myChr = ‘yeehah’

To be more robust and avoid size issues, you should use a string array, which you create with a *dim command:

*DIM,myString,STRING,80

This would create a text string called myString that is 80 characters long.  And you would do this if you are going to use a function to fill it.  Once you create a string you refer to it with its name and an index for the start of the string.

/title, Working on Model %myString(1)%

For a character parameter, you don’t need the index:

/title,Working on Model %myChr%

Stuff you should know about strings in APDL:

  • You define the value of a string by putting the text in single quotes ‘string’
  • The longest string you can have is 256 character – but don’t worry, you can string strings together.
  • String arrays can be two dimensional, so if you have a bunch of text you want to keep together you can do that fairly easily.  You do have to use *dim if you do this:
    • *dim,comp,string,80,  3       [Fixed on 11/19/14 ]
      comp(1,1) = ‘leftSide’
      comp(1,2)=’rightside’
      comp(1,3)=’topside’
      *do,I,1,3
      cmsel,a,%comp(1,i)%
      *enddo
  • You can make an array of type Char as well. This is basically a list of several character parameters.
  • To look at what is in your array/parameter, use *stat.

String Functions

Money is the string with which a sardonic destiny directs the motions of its puppets. – W. Sumerset Maugham

Now on to the fun stuff!

Once you have a string you can manipulate it using the string functions.  We will look at each group.

Strings to and from Numbers

Sometimes you have a number as a string and you need it as a number, or you need to convert a number into a string.  Those numbers can be decimal or Octal or Hex.  This Table shows the functions for going back and forth.

Function

Description

VALCHR(string)Converts a string that contains a valid  decimal or integer number as text into a double precision parameter
VALOCT(string)Converts a string that contains a valid  Octal number as text into a double precision parameter
VALHEX(string)Converts a string that contains a valid  Hexadecimal number as text into a double precision parameter
CHRVAL(num)Converts a decimal number into a text string
CHROCT(num)Converts a number into a text string octal number
CHRHEX(num)Converts a number into a text string hexahedral number

The most commonly used function in this group is CHARVAL() to truncate the number of decimals in a number.

Standard String Functions

If you have every programed in FORTRAN, C or any other “old school” language you are familiar with the standard set of commands that are used to manipulate strings. APDL supports most of them:

Function

Description

STRSUB(Str1,nLoc,nChar)Returns a substring of the characters in Str1.  Returns characters number nLoc to nLoc+nChar as a string.
STRCAT(Str1,Str2)Concatenates Str2 onto the end of Str1. Returns a string.
STRFILL(Str1,Str2,nLoc)Fill in Str1 with Str2 starting at character number nLoc in Str1. Returns a string.
STRCOMP(Str1)Compress Str1 by removing all the spaces in Str1. Returns a string.
STRLEFT(Str1)Left justify Str1 by removing all leading spaces from Str1. Returns a string.
STRPOS(Str1,Str2)Returns the position in Str1 where Str2 can be found.  If it is not found, it returns 0.  Returns a number.
STRLENG(Str1)Returns the length of Str1, excluding training spaces. Returns a number.
UPCASE(Str1)Converts all the characters in Str1 to Upper Case. Returns a string.
LWCASE(Str1)Converts all the characters in Str1 to Lower Case. Returns a string.

These are fairly standard and work as you would expect.

File Name Functions

The last two functions are there for you to work with file names and directories.  There are three parts to a path name in ANSYS MAPDL:  the directory where the file is (directory), the portion of the filename before the period (filename) and the part after the period (ext).  The first function allow you to build a complete string that specifies a file and the second takes a complete path and divides it into its pieces.

Function

Description

JOIN(directory,filename,extension)Creates a path string in the current OS (forward vs. backward slashes) from the supplied directory, filename, and extension.  Directory is optional.
Split(Path,Option)Takes a full pathname in the current OS and splits it up into pieces. Option can be:
DIR = give just the directory
FILE = Give the filename and extension
Name = just the filename, the part before the period
EXT = give the extension, the part after the period.

Using Strings in APDL

There is geometry in the humming of the strings, there is music in the spacing of the spheres. – Pythagoras

The examples above sort of hint at how to use strings in APDL but it is probably a good idea to explicitly talk about it.  You can use strings in commands in clever ways, some commands use strings as arguments, and you can write strings to files.

String Substitution in a Command

This is the most common usage and the most powerfully.  Any CHAR or STRING parameter can be put into any APDL command by simply putting it inside percent signs:  %myString%

So you can do something like:

/title, Results for Substep %isbstp%.  Freq = %frq%

Some people get very sophisticated with this and build their own commands this way.  Do note that this substitution also works for numbers.  cmsel,s,ncmp%i%  where i = 32 will look to the program like cmsel,s,ncmp32.

Strings as Arguments

This is fairly straightforward, if a command is looking for a string, give it a string.  There are not too many commands that take strings as arguments but there are a few.

Writing Strings to Files

There are three basic ways to write to a file in APDL:  *create, *cwrite, and *vwrite.  We are not going to get into there use here, but check out the usage of each in detail in the help.

Conclusions

There are strings in the human heart that had better not be vibrated. – Charles Dickens

This has been a very brief overview of a powerful tool in ANSYS Mechanical APDL. Take a look at the help and other resources (xansys.org, ansys.net) to learn more and just start using it.  Fun stuff.

Categories

Certified Elite Channel Partner

Get Your Ansys Products & Support from the Engineers who Contribute to this Blog.

Product Development
Diamond Partner

Technical Expertise to Enable your Addictive Manufacturing Success.

PADT’s Pulse Newsletter

Keep up to date on what is going on at PADT by subscribing to our newsletter.


By submitting this form, you are consenting to receive marketing emails from: Phoenix Analysis and Design Technologies, 7755 S. Research Dr., Tempe, AZ, 85284, https://www.padtinc.com. You can revoke your consent to receive emails at any time by using the SafeUnsubscribe® link, found at the bottom of every email. Emails are serviced by Constant Contact

Share this post:

Share on twitter
Share on facebook
Share on linkedin
Share on pinterest

Upcoming Events

05/19/2022

RAPID + tct 2022

05/19/2022

Venture Cafe Roundtable: AI & Healthcare

05/18/2022

Tucson after5 Tech Mixer: World View

05/18/2022

RAPID + tct 2022

More Info

05/18/2022

Signal & Power Integrity Updates in Ansys 2022 R1 - Webinar

05/18/2022

Simulation World 2022

05/17/2022

RAPID + tct 2022

05/11/2022

Experience Stratasys Manufacturing Virtual Event

05/04/2022

Mechanical Meshing Updates in Ansys 2022 R1 - Webinar

04/27/2022

04/22/2022

12TH ANNUAL TUCSON GOLF TOURNAMENT

04/21/2022

04/20/2022

Additional Fluids Updates in Ansys 2022 R1

04/20/2022

Experience Stratasys Tour – Tempe Arizona

04/18/2022

Experience Stratasys Tour - Flagstaff Arizona

04/14/2022

D&M West | MD&M West

04/13/2022

D&M West | MD&M West

04/13/2022

Experience Stratasys Tour - Albuquerque New Mexico

04/12/2022

D&M West | MD&M West

04/12/2022

Experience Stratasys Tour - Los Alamos New Mexico

04/12/2022

Optimizing Engineering Workflows f​​​​or Propulsion System Design

04/07/2022

Experience Stratasys Tour - Austin Texas

04/07/2022

37th Space Symposium - Arizona Space Industry

04/06/2022

Transforming Digital Engineering with Ansys Discovery 2022 R1

04/06/2022

37th Space Symposium - Arizona Space Industry

04/05/2022

37th Space Symposium - Arizona Space Industry

04/04/2022

37th Space Symposium - Arizona Space Industry

03/30/2022

Simulation Best Practices for Vehicle Engineering - Webinar

03/23/2022

03/23/2022

High & Low Frequency Electromagnetics Updates in Ansys 2022 R1

02/24/2022

Arizona Technology Council After 5 Tech Mixer "Pandemic Pivot Pizza Pa

02/23/2022

SciTech Festival: Spend an Hour with 3D Printing Experts

02/11/2022

Webinar: Mechanical overview for Ansys 2022 R1

More Info

02/09/2022

Webinar: Product Development 101 (FAKE)

02/08/2022

Webinar: Navigating the Additive Landscape

01/27/2022

Arizona Technology Council 1st Quarter VIP Tech Mixer

More Info

01/26/2022

Simulation Best Practices for Gas Turbine Design & Development - Webin

More Info

01/19/2022

Arizona Photonics Days

More Info

11/04/2021

ExperienceIT, New Mexico

More Info

11/03/2021

Additive Manufacturing & Structural Optimization in Ansys 2021 R2 - We

More Info

11/03/2021

Optics Valley Technical Series: The Future of Simulation in the Optics

More Info

11/02/2021

SBIR Liftoff AZTC Virtual Breakfast Series

More Info

10/10/2021

Stratasys Mobile Truck Stop - Tucson Arizona

More Info

Search in PADT site

Contact Us

Most of our customers receive their support over the phone or via email. Customers who are close by can also set up a face-to-face appointment with one of our engineers.

For most locations, simply contact us: