Issue
I want to check if a string provided by the user is suitable for a block name or for a layer name. Is there any function that can validate the string?
Solution
You are right, not every name is acceptable, for example, for a block. When creating a block in the AutoCAD UI, the user can get the following error message:
As you may know, there is a number of Symbol Tables in an dwg file, like Block table, Linetype table etc.
To check if a given string is a valid symbol table name or not, in ObjectARX you can use acdbSNValid() function.
AutoCAD .NET API provides the SymbolUtilityServices.ValidateSymbolName() for the same purpose. The method throws an exception when the name is invalid. Here is an example:
string[] names = { "lk3j4!@ #`$>%", // will cause an exception
"lk3j4!@ #$%" }; // will work fine
foreach (string s in names)
{
try
{
// Validate the provided symbol table name
SymbolUtilityServices.ValidateSymbolName( s, false);
System.Windows.Forms.MessageBox.Show( s + " is a valid name." );
}
catch
{
// An exception has been thrown, indicating that
// the name is invalid
System.Windows.Forms.MessageBox.Show( s +" is an invalid name.");
}
} // foreach
The 2nd parameter of the ValidateSymbolName() is a flag that allows or disallows the '|' symbol (so called "pipe" symbol).