TheSwamp

Code Red => ARX Programming => Topic started by: lethuongtri on January 22, 2022, 07:41:17 AM

Title: Can not Attach Mtext to Leader
Post by: lethuongtri on January 22, 2022, 07:41:17 AM
Hello everybody . I got a question . So that now I'm creating leader with annotation(Mtext).
The source code is like below .I used "pLeader->attachAnnotation(pMtext->objectID) " but it didn't work.Can you tell me why ?
about Command 's result, I also attached the image below . Please see it.
Code: [Select]
void
cmdKRLE()
{
    ads_point pt0,pt1, pt2;
    TCHAR Content[100];

    pt0[0] = 0;
    pt0[1] = 0;
    pt0[2] = 0;

    acedInitGet(1, NULL);
    acedGetPoint(pt0, _T("\n Select the first point:"), pt1);

    acedInitGet(1, NULL);
    acedGetPoint(pt1, _T("\n Select the second point:"), pt2);

    acdbUcs2Wcs(pt1, pt1, false);
    acdbUcs2Wcs(pt2, pt2, false);
   
    AcDbDatabase* pDb = acdbHostApplicationServices()->workingDatabase();

    Acad::ErrorStatus es;
    AcDbBlockTableRecordPointer pBtr(pDb->currentSpaceId(), AcDb::kForWrite);
    if (pBtr.openStatus() != Acad::eOk) return;

    struct resbuf DimScale;
    acedGetVar(_T("DIMSCALE"), &DimScale);

    acedGetString(1, _T("\n Input content:"), Content);
    AcDbObjectPointer <AcDbMText> pMtext;
    pMtext.create();
    pMtext->setContents(Content);
    pMtext->setLayer(_T("TEXT"));
    pMtext->setTextHeight(DimScale.resval.rreal * 3.2);
    pMtext->setLocation(asPnt3d(pt2));
    if (pt2[0] >= pt1[0])
    {
        pMtext->setAttachment(AcDbMText::kBottomLeft);
    }
    else
    {
        pMtext->setAttachment(AcDbMText::kBottomRight);
    }
    es = pBtr->appendAcDbEntity(pMtext);

    if (es != Acad::eOk)
    {
        acutPrintf(_T("\n Can not add Mtext!"));
        return;
    }
    AcDbObjectPointer<AcDbLeader> pLeader;
    pLeader.create();
    pLeader->appendVertex(asPnt3d(pt1));
    pLeader->appendVertex(asPnt3d(pt2));
    es = pBtr->appendAcDbEntity(pLeader);
    if (es != Acad::eOk)
    {
        acutPrintf(_T("\n Can not add Leader!"));
        return;
    }
    pLeader->setLayer(_T("SCALE"));
    pLeader->setDimscale(DimScale.resval.rreal);
    pLeader->attachAnnotation(pMtext->objectId());
    pLeader->evaluateLeader();
}
Title: Re: Can not Attach Mtext to Leader
Post by: It's Alive! on January 22, 2022, 07:13:55 PM
Hi,

You should check the error status of every ARX call, likely it will tell when things go bad.

example

Code - C: [Select]
  1.  if (auto e = pMtext->setContents(...); e != eOk)
  2.     acutPrintf(_T("\nError @ pMtext->setContents = %ls: "), acadErrorStatusText(e));
  3.  

Also, I like to use set database defaults after creating entities, It might not be the error here, but its the cause of a lot of issues

Code - C: [Select]
  1.         AcDbObjectPointer <AcDbMText> pMtext;
  2.         pMtext.create();
  3.         pMtext->setDatabaseDefaults(pDb);
  4.  
Title: Re: Can not Attach Mtext to Leader
Post by: lethuongtri on January 22, 2022, 08:42:31 PM
Thank for your reply . I got this . Let me check my code again with  this.