Author Topic: Rename TextStyle with Visual LISP  (Read 4059 times)

0 Members and 1 Guest are viewing this topic.

Lee Mac

  • Seagull
  • Posts: 12514
  • London, England
Rename TextStyle with Visual LISP
« on: August 20, 2011, 09:59:21 AM »
Maybe I'm overlooking something here, but is it possible? If so, how?

LE3

  • Guest
Re: Rename TextStyle with Visual LISP
« Reply #1 on: August 20, 2011, 12:13:53 PM »
not that i remember - the name method it is marked as (RO) attribute

Jeff H

  • Needs a day job
  • Posts: 6103
Re: Rename TextStyle with Visual LISP
« Reply #2 on: August 20, 2011, 12:22:50 PM »
Of course I do not know but out of curiosity can you use TableSearch to grab the textstyle and change the name?
 
In lisp is it not the same as grabbing any symbolTableRecord(Blocks etc..) and changing the name?

LE3

  • Guest
Re: Rename TextStyle with Visual LISP
« Reply #3 on: August 20, 2011, 12:23:28 PM »
and just to what they have in the help:
Quote
New text inherits height, width factor, obliquing angle, and text generation properties from the current text style. To create a text style, use the Add method to create a new TextStyle object and add it to the TextStyles collection. The Add method takes a TextStyle name as input. Once created, you cannot change the name of a text style through AutoCAD ActiveX Automation.

hth.-

efernal

  • Bull Frog
  • Posts: 203
Re: Rename TextStyle with Visual LISP
« Reply #4 on: August 20, 2011, 12:24:15 PM »
this?
Code: [Select]
(IF (TBLSEARCH "STYLE" "NEW-TESTE")
  (COMMAND "-RENAME" "STYLE" "NEW-TESTE" "NEW-TEST")
)
[code]
e.fernal

Lee Mac

  • Seagull
  • Posts: 12514
  • London, England
Re: Rename TextStyle with Visual LISP
« Reply #5 on: August 20, 2011, 12:59:38 PM »
Cheers guys, I thought it was a long shot as I had also read those parts of the Help Docs before posting, but I also thought there was a small chance that one of you clever guys had figured a way around it  :-)

Entmod'ing the tblobjname entity works, so I'll go that route  :-)

Cheers!

Lee

LE3

  • Guest
Re: Rename TextStyle with Visual LISP
« Reply #6 on: August 20, 2011, 01:07:38 PM »
Cheers guys, I thought it was a long shot as I had also read those parts of the Help Docs before posting, but I also thought there was a small chance that one of you clever guys had figured a way around it  :-)

Entmod'ing the tblobjname entity works, so I'll go that route  :-)

Cheers!

Lee

See, you hit another limitation of lisp/vlisp or simple because was not exposed... have a look if you get a chance to this quick extension i just did:

Code: [Select]
static int ads_renametextstyle(void)
{
struct resbuf *pArgs =acedGetArgs () ;
Acad::ErrorStatus es;
ACHAR pTextNameOrig [256];
ACHAR pTextNameNew [256];
if (pArgs && (pArgs->restype == RTSTR) && pArgs->rbnext && (pArgs->rbnext->restype == RTSTR))
{
_tcscpy(pTextNameOrig, pArgs->resval.rstring);
_tcscpy(pTextNameNew, pArgs->rbnext->resval.rstring);
AcDbTextStyleTableRecordPointer pTextStyle(pTextNameOrig, acdbCurDwg(), AcDb::kForWrite);
if (pTextStyle.openStatus() == Acad::eOk)
{
if (pTextStyle->setName(pTextNameNew) == Acad::eOk)
{
acedRetT();
}
else
{
acedRetNil();
}
}
else
{
acedRetNil();
}
}
else
{
acedRetNil();
}
return (RSRSLT) ;
}

HTH
« Last Edit: August 20, 2011, 03:04:29 PM by DeVo »

Lee Mac

  • Seagull
  • Posts: 12514
  • London, England
Re: Rename TextStyle with Visual LISP
« Reply #7 on: August 20, 2011, 01:15:12 PM »
See, you hit another limitation of lisp/vlisp or simple because was not exposed...

True, and I would think the limitations will become increasingly apparent as the ACAD versions progress...

Many thanks for the code Luis and insight into how this could be overcome though, very much appreciated  :-)

Jeff H

  • Needs a day job
  • Posts: 6103
Re: Rename TextStyle with Visual LISP
« Reply #8 on: August 20, 2011, 03:41:31 PM »
Quick C# version
 
Code: [Select]
         [LispFunction("Netlsp-ChangeTextStyleName")]
        public static ResultBuffer ChangeTextStyleName(ResultBuffer resultBufferIn)
        {
            TypedValue[] lispArgs = resultBufferIn.AsArray();
            string textStyleName = (string)lispArgs[0].Value;
            string newTextStyleName = (string)lispArgs[1].Value;
            Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction trx = db.TransactionManager.StartTransaction())
            {
                TextStyleTable textStyleTbl = (TextStyleTable)trx.GetObject(db.TextStyleTableId, OpenMode.ForRead);
                TextStyleTableRecord textStyleTableRecord = (TextStyleTableRecord)trx.GetObject(textStyleTbl[textStyleName], OpenMode.ForWrite);
                textStyleTableRecord.Name = newTextStyleName;
                trx.Commit();
                return new ResultBuffer(new TypedValue((int)LispDataType.T_atom));
            }
        }

C# version that allows third parameter of T or nil.
If T it returns text explaining the error for example if:
 parameters are wrong, the textsytle does not exist, the new name already exist etc......
if nil will return nil if errors ot if does not exist etc....
Code: [Select]
[LispFunction("Netlsp-ChangeTextStyleName")]
        public static ResultBuffer ChangeTextStyleName(ResultBuffer resultBufferIn)
        {
            TypedValue[] lispArgs = resultBufferIn.AsArray();
            bool returnErrorString = false;
            if (lispArgs[2].TypeCode == (short)LispDataType.T_atom)
            {
                returnErrorString = true;
            }
            if (lispArgs.Length > 3 || lispArgs.Length < 2)
            {
                if (returnErrorString)
                {
                    return new ResultBuffer(new TypedValue((int)LispDataType.Text, "Incorrect Number Paramteres"));
                }
                else
                {
                    return new ResultBuffer(new TypedValue((int)LispDataType.Nil));
                }
            }
            if (!(lispArgs[0].TypeCode == (short)LispDataType.Text))
            {
                if (returnErrorString)
                {
                    return new ResultBuffer(new TypedValue((int)LispDataType.Text, "First Parameter is not a string"));
                }
                else
                {
                    return new ResultBuffer(new TypedValue((int)LispDataType.Nil));
                }
            }
            if (!(lispArgs[1].TypeCode == (short)LispDataType.Text))
            {
                if (returnErrorString)
                {
                    return new ResultBuffer(new TypedValue((int)LispDataType.Text, "Second Parameter is not a string"));
                }
                else
                {
                    return new ResultBuffer(new TypedValue((int)LispDataType.Nil));
                }
            }
            string textStyleName = (string)lispArgs[0].Value;
            string newTextStyleName = (string)lispArgs[1].Value;
            Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction trx = db.TransactionManager.StartTransaction())
            {
                TextStyleTable textStyleTbl = (TextStyleTable)trx.GetObject(db.TextStyleTableId, OpenMode.ForRead);
                if (!textStyleTbl.Has(textStyleName))
                {
                    if (returnErrorString)
                    {
                        return new ResultBuffer(new TypedValue((int)LispDataType.Text, "The TextStye does not exist"));
                    }
                    else
                    {
                        return new ResultBuffer(new TypedValue((int)LispDataType.Nil));
                    }
                }
                if (textStyleTbl.Has(newTextStyleName))
                {
                    if (returnErrorString)
                    {
                        return new ResultBuffer(new TypedValue((int)LispDataType.Text, "The TextStye already exist"));
                    }
                    else
                    {
                        return new ResultBuffer(new TypedValue((int)LispDataType.Nil));
                    }
                }

                TextStyleTableRecord textStyleTableRecord = (TextStyleTableRecord)trx.GetObject(textStyleTbl[textStyleName], OpenMode.ForWrite);
                textStyleTableRecord.Name = newTextStyleName;
                trx.Commit();
                return new ResultBuffer(new TypedValue((int)LispDataType.T_atom));
            }
        }