Author Topic: Nested copy  (Read 10587 times)

0 Members and 2 Guests are viewing this topic.

MickD

  • Gator
  • Posts: 3314
  • (x-in)->[process]->(y-out)
Re: Nested copy
« Reply #15 on: March 24, 2006, 12:20:29 AM »
Actually, that's just part of the problem.
If you copy the block in the same drawing it will crash straight up.
I haven't done much with blocks or xref's so I'm not much help I'm afraid :(
Forth is like the Tao: it is a Way, and is realized when followed.
Its fragility is its strength; its simplicity is its direction - Michael Ham

Lao Tzu: “To attain knowledge, add things
every day; to obtain wisdom, remove things every day.”

LE

  • Guest
Re: Nested copy
« Reply #16 on: March 24, 2006, 12:26:54 AM »
Mick;

Try this code, this one works... it needs the transformation....

Code: [Select]
    ads_name ss;
    if (RTNORM != acedSSGet( ":N", NULL, NULL, NULL, ss ))
    {
        acutPrintf( "acedSSGet cancelled\n" );
        return;
    }

    long length;
    if (RTNORM != acedSSLength( ss, &length ) || 0 == length)
    {
        acutPrintf( "empty selection set\n" );
        acedSSFree( ss );
        return;
    }

    ads_name          ent;
    AcDbObjectId      id, objId1, objId2;
    AcDbObjectIdArray list;
    AcDbIdMapping     pIdMap;

struct resbuf *pRb;

for (long i = 0; i < length; i++) {
        if (RTNORM != acedSSNameX(&pRb, ss, i)) return;
            continue;

   struct resbuf *pTemp;

    int i;
    for (i=1, pTemp = pRb;i<3;i++, pTemp = pTemp->rbnext)
        { ; }
    ads_name ename;
    ads_name_set(pTemp->resval.rlname, ename);

        if (Acad::eOk != acdbGetObjectId( id, ename )) return;
            continue;

        list.append( id );
    }
    acedSSFree( ss );
   AcDbObjectId blockId = getBlockId( ACDB_MODEL_SPACE );
    if (blockId != 0) {
        if (Acad::eOk != acdbHostApplicationServices()->workingDatabase()
            ->deepCloneObjects( list, blockId, pIdMap ))
            acutPrintf( "Error on deepCloneObjects\n" );
    }
    else
        acutPrintf( "Error in getting ModelSpace ID\n" );

Good night!

Alexander Rivilis

  • Bull Frog
  • Posts: 212
  • Programmer from Kyiv (Ukraine)
Re: Nested copy
« Reply #17 on: March 24, 2006, 08:39:42 AM »
 :-) Try this code:
Code: [Select]
static void NestedCopy(void)
{
  Acad::ErrorStatus es;
  ads_name ss;
  if (acedSSGet(":N",NULL,NULL,NULL,ss) != RTNORM) {
    acutPrintf("\nSelection canceled!"); return;
  }
  long nlen = 0;
  if (acedSSLength (ss,&nlen) != RTNORM || nlen == 0) {
    acutPrintf("\nNot selected!"); return;
  }
  resbuf *rbn=NULL,*rb;
  for (int i=0; i < nlen; i++) {
    if (acedSSNameX(&rbn,ss,i) != RTNORM) {
      acutPrintf("\nacedSSNameX error!"); return;
    }
    AcDbObjectId objId;
    AcDbObjectIdArray objIds;
    for (rb=rbn; rb; rb = rb->rbnext) {
      if (rb->restype != RTENAME || acdbGetObjectId (objId,rb->resval.rlname) != Acad::eOk)
        continue;
      objIds.append (objId);
    }
    acutRelRb (rbn);       
    if (objIds.length () == 0) return;
    objId = objIds[0];
    objIds.removeFirst();
    AcDbObjectPointer<AcDbEntity> pEnt(objId,AcDb::kForRead);
    if ((es = pEnt.openStatus ()) != Acad::eOk) {
      acutPrintf("\npEnt.openStatus ()=%s", acadErrorStatusText (es));
      return;
    }
    AcDbEntity *pNewEnt = static_cast<AcDbEntity *>(pEnt->clone());
    if (AcDbAttribute::cast (pNewEnt)) {
      //--------------------------------------------------
      // We can not add Attribute directly to database -
      // but we can add Text instead Attribute  :) !!!
      //--------------------------------------------------
      AcDbAttribute *pAtt = AcDbAttribute::cast (pNewEnt);
      AcDbText *pText = new AcDbText();
      pText->setPropertiesFrom(pAtt);
      pText->setAlignmentPoint(pAtt->alignmentPoint());
      pText->setHeight(pAtt->height ());
      pText->setWidthFactor(pAtt->widthFactor());
      pText->setHorizontalMode(pAtt->horizontalMode());
      pText->setTextStyle(pAtt->textStyle());
      pText->setVerticalMode(pAtt->verticalMode());
      pText->setTextString(pAtt->textString());
      pText->setRotation(pAtt->rotation());
      pText->setPosition (pAtt->position());
      pText->setNormal(pAtt->normal());
      pText->setOblique(pAtt->oblique());
      pText->setThickness(pAtt->thickness());
      delete pNewEnt;
      pNewEnt = pText;
    }
    AcGeMatrix3d xform;
    int j = objIds.length ();
    for (int i=0; i < objIds.length (); i++) {
      AcDbObjectPointer<AcDbBlockReference> pRef(objIds[i],AcDb::kForRead);
      if (pRef.openStatus () == Acad::eOk) {
        xform = pRef->blockTransform ();
        AcDbBlockTableRecordPointer pBtr(pRef->blockTableRecord(),AcDb::kForRead);
        if (pBtr.openStatus () == Acad::eOk) {
          AcGePoint3d pt3dBlkOrigin = pBtr->origin();
          if (pt3dBlkOrigin != AcGePoint3d::kOrigin) {
            AcGeVector3d xformOrigin = xform.translation();
            AcGeVector3d vectorOrigin(pt3dBlkOrigin.x,pt3dBlkOrigin.y,pt3dBlkOrigin.x);
            vectorOrigin.transformBy(xform);
            xformOrigin -= vectorOrigin;
            xform.setTranslation(xformOrigin);
          }
        }
        if ((es = pNewEnt->transformBy (xform)) != Acad::eOk) {
          acutPrintf("\npNewEnt->transformBy (xform)=%s", acadErrorStatusText (es));
        }
      }
    }

    if (postToDatabase (acdbCurDwg()->currentSpaceId(),pNewEnt)) {
      pNewEnt->close ();
    } else {
      delete pNewEnt;
    }
  }
}
« Last Edit: March 24, 2006, 10:18:40 AM by Rivilis »

LE

  • Guest
Re: Nested copy
« Reply #18 on: March 24, 2006, 10:22:58 AM »
Hello Alexander;

I tested the code and nope it is still crashing AutoCAD, [well without doing a debug]

I have the function postToDatabase() - it is correct?

Code: [Select]
Acad::ErrorStatus
postToDatabase(AcDbObjectId& idObj, AcDbEntity* pEnt)
{
  Acad::ErrorStatus es;
  AcDbBlockTable* pBlockTable;
  AcDbBlockTableRecord* pSpaceRecord;
  if (acdbHostApplicationServices()->workingDatabase()==NULL) return Acad::eNoDatabase;
  if ((es = acdbHostApplicationServices()->workingDatabase()->getBlockTable(pBlockTable, AcDb::kForRead))==Acad::eOk)
  {
    if ((es = pBlockTable->getAt(ACDB_MODEL_SPACE, pSpaceRecord, AcDb::kForWrite))==Acad::eOk)
    {
      if ((es = pSpaceRecord->appendAcDbEntity(idObj, pEnt))==Acad::eOk) pEnt->close();
      pSpaceRecord->close();
    }
    pBlockTable->close();
  }
  return es;
}

Thanks!

Alexander Rivilis

  • Bull Frog
  • Posts: 212
  • Programmer from Kyiv (Ukraine)
Re: Nested copy
« Reply #19 on: March 24, 2006, 10:27:44 AM »
I am using that function:
Code: [Select]
bool postToDatabase (AcDbObjectId blockId, AcDbEntity *pEntity) {
  assert( pEntity != NULL ) ;
  AcDbBlockTableRecordPointer pBlk (blockId, AcDb::kForWrite) ;
  if ( (es = pBlk.openStatus ()) != Acad::eOk ) {
    acutPrintf ("\npBlk.openStatus ()=%s",acadErrorStatusText (es));
    return (false) ;
  }
  Acad::ErrorStatus es = pBlk->appendAcDbEntity (pEntity);
  if (es != Acad::eOk) {
    acutPrintf ("\npBlk->appendAcDbEntity (pEntity)=%s",acadErrorStatusText (es));
  }
  return (es == Acad::eOk) ;
}
Try with mine function - your's function  try to change acdbCurDwg()->currentSpaceId() ! ;)
« Last Edit: March 24, 2006, 10:33:40 AM by Rivilis »

LE

  • Guest
Re: Nested copy
« Reply #20 on: March 24, 2006, 10:39:32 AM »
That it is great, thanks it works!

I just did some minor changes to your function:

Code: [Select]
bool postToDatabase (AcDbObjectId blockId, AcDbEntity *pEntity) {
  assert( pEntity != NULL ) ;
  Acad::ErrorStatus es;
  AcDbBlockTableRecordPointer pBlk (blockId, AcDb::kForWrite) ;
  if ( (es = pBlk.openStatus ()) != Acad::eOk ) {
    acutPrintf ("\npBlk.openStatus ()=%s",acadErrorStatusText (es));
    return (false) ;
  }
  es = pBlk->appendAcDbEntity (pEntity);
  if (es != Acad::eOk) {
    acutPrintf ("\npBlk->appendAcDbEntity (pEntity)=%s",acadErrorStatusText (es));
  }
  return (es == Acad::eOk) ;
}

Rock & Roll

Alexander Rivilis

  • Bull Frog
  • Posts: 212
  • Programmer from Kyiv (Ukraine)
Re: Nested copy
« Reply #21 on: March 24, 2006, 10:43:24 AM »
Ok! I edit this function in-place (in forum) - and do mistake.
 :-)

LE

  • Guest
Re: Nested copy
« Reply #22 on: March 24, 2006, 11:26:32 AM »
Ok! I edit this function in-place (in forum) - and do mistake.
 :-)

Вы - преподаватель в ObjectARX, и я учусь из Вас, спасибо.

 :-)

Alexander Rivilis

  • Bull Frog
  • Posts: 212
  • Programmer from Kyiv (Ukraine)
Re: Nested copy
« Reply #23 on: March 24, 2006, 11:32:05 AM »
Вы - преподаватель в ObjectARX, и я учусь из Вас, спасибо.
 :-)

Вы - преподаватель ObjectARX и я учусь у Вас. Спасибо. - Some errors corrections. :-D