Author Topic: string comparison and for loop iteration  (Read 2856 times)

0 Members and 1 Guest are viewing this topic.

Draftek

  • Water Moccasin
  • Posts: 1501
string comparison and for loop iteration
« on: May 24, 2006, 04:01:13 PM »
I've built this method to manipulate a text style for my new table object. I don't think my method of comparing the names in the iterator loop is right. Can't put my finger on it, but it just doesn't seem like the best way to do this. Also what about the way I'm using the bool in the for loop:

Code: [Select]
// set and get the windows text style I need
Acad::ErrorStatus SetTextStyle(const ACHAR *styleName, const ACHAR *winFontName,
                               AcDbObjectId& styleID, AcDbDatabase *pDb)
{
Acad::ErrorStatus es;
AcDbTextStyleTableRecord *pRecord = NULL;
ACHAR *testName;
// these are all mainly just placeholders for output on the methods used below
AcDbTextStyleTable *pTextStyleTable = NULL;
Adesk::Boolean isBold = Adesk::kFalse;
Adesk::Boolean isItalic = Adesk::kFalse;
int iCharset = 1;
int iFamily = 1;
// eh, Just in case
if(!pDb)
{
pDb = acdbHostApplicationServices()->workingDatabase();
}
// open the textstyle symbol table for querying
if((es = pDb->getTextStyleTable(pTextStyleTable, AcDb::kForRead)) != Acad::eOk)
{
acutPrintf(_T("\nCan't open the text style table"));
return es;
}
// use the iterator
AcDbTextStyleTableIterator *pIterator;
pTextStyleTable->newIterator(pIterator);
bool hasStyle = false;
for (; !pIterator->done(); pIterator->step())
{
pIterator->getRecord(pRecord, AcDb::kForWrite);
pRecord->getName(testName);
USES_CONVERSION;
if ((strcmp(T2A(testName), T2A(styleName))) == 0)
{
// style already exists so just force the font we want
hasStyle = true;
pRecord->setFont(winFontName, isBold, isItalic, iCharset, iFamily);
break;
}
pRecord->close();
}
// clean up the iterator or problems abound
delete pIterator;
// if the style didn't exist then create it
if (!hasStyle)
{
pRecord = new AcDbTextStyleTableRecord();
pTextStyleTable->upgradeOpen();
pRecord->setFont(winFontName, isBold, isItalic, iCharset, iFamily);
pRecord->setName(styleName);
if((es = pTextStyleTable->add(pRecord)) != Acad::eOk)
{
acutPrintf(_T("\nCouldn't add the text style record"));
pRecord->close();
pTextStyleTable->close();
return es;
}
}
// get the objectId to pass back to the calling client
styleID = pRecord->objectId();
pRecord->close();
pTextStyleTable->close();
return es;
}

Any suggestions?
Note: This is AutoCAD2007, hence the _T macro and ACHAR variable type.
Soli Deo Gloria

MickD

  • Gator
  • Posts: 3285
  • (x-in)->[process]->(y-out)
Re: string comparison and for loop iteration
« Reply #1 on: May 24, 2006, 06:18:43 PM »
You can save yourself some trouble using the 'symboltableName'::has(), ::getAt() and ::add() functions associated with all symbol tables.
Do a search in help for the has() function ;)
hth,
Mick.
Debugging:
Being the detective in a crime movie where you're also the murderer.

“Someone's sitting in the shade today because someone planted a tree a long time ago.”
- Warren Buffet

Draftek

  • Water Moccasin
  • Posts: 1501
Re: string comparison and for loop iteration
« Reply #2 on: May 25, 2006, 08:07:11 AM »
Thanks Mick,

I should have mentioned that I started out doing just that. The problem was, If the style existed the function did not return a pointer to the tablestylerecord and I needed it to adjust the font.

Of course, I could have required the calling method to do that but in the instance I needed to create the style I needed it passed anyway.

hmm. I guess I could have extracted the object from the objectID..
Soli Deo Gloria

MickD

  • Gator
  • Posts: 3285
  • (x-in)->[process]->(y-out)
Re: string comparison and for loop iteration
« Reply #3 on: May 25, 2006, 05:48:07 PM »
Yes you could, but why can't you just (psuedo code) -

If it **->has()
{
      pRec = **->getAt();
}
else
{
     pRec = **->add();
}

do something with pRec....
Debugging:
Being the detective in a crime movie where you're also the murderer.

“Someone's sitting in the shade today because someone planted a tree a long time ago.”
- Warren Buffet

Chuck Gabriel

  • Guest
Re: string comparison and for loop iteration
« Reply #4 on: May 25, 2006, 06:32:36 PM »
Give this a try.  It compiles, but I haven't tested it.

Code: [Select]
// set and get the windows text style I need
Acad::ErrorStatus SetTextStyle(const TCHAR* styleName,
                                             const TCHAR* winFontName,
                                             AcDbObjectId& styleID,
                                             AcDbDatabase* pDb) {

// eh, Just in case
if(!pDb) {
pDb = acdbHostApplicationServices()->workingDatabase();
}

AcDbTextStyleTableRecordPointer style(styleName, pDb, AcDb::kForWrite);
if(style.openStatus() == Acad::eOk) {
return style->setFont(winFontName, false, false, 1, 1);
} else {
Acad::ErrorStatus es = style.create();
if(es != Acad::eOk) return es;
es = style->setName(styleName);
if(es != Acad::eOk) return es;
es = style->setFont(winFontName, false, false, 1, 1);
if(es != Acad::eOk) return es;
AcDbTextStyleTablePointer styleTable(pDb, AcDb::kForWrite);
es = styleTable.openStatus();
if(es != Acad::eOk) return es;
es = styleTable->add(styleID, style.object());
if(es != Acad::eOk)
acutPrintf(_T("\nCouldn't add the text style record"));
return es;
}
}

Draftek

  • Water Moccasin
  • Posts: 1501
Re: string comparison and for loop iteration
« Reply #5 on: May 26, 2006, 08:04:46 AM »
I see Mick, good idea.

I'll try that Chuck, Thanks. Nice and compact.
Soli Deo Gloria