Here are the code snippets to create aligned and ordinate dimensions in paperspace while retaining their associativity with a reference point on an entity that is in modelspace.
//For AcDbDimAssoc
#include "dbdimassoc.h"
//For AcDbOsnapPointRef
#include "dbdimptref.h"
static void paperRotatedDimAssoc(void )
{
AcDbDatabase* pDb
= acdbHostApplicationServices()->workingDatabase();
AcDbBlockTableRecordPointer ms(
ACDB_MODEL_SPACE, pDb, AcDb::kForWrite);
AcDbBlockTableRecordPointer ps(
ACDB_PAPER_SPACE, pDb, AcDb::kForWrite);
AcDbObjectId lineId, vpId, dimId;
AcGePoint3d pt1(AcGePoint3d::kOrigin);
AcGePoint3d pt2(5,0,0);
{//creates a line
AcDbObjectPointer<AcDbLine> line;
line.create();
line->setStartPoint(pt1);
line->setEndPoint(pt2);
ms->appendAcDbEntity(lineId, line);
}
{//creates a viewport
AcDbObjectPointer<AcDbViewport> vp;
vp.create();
ps->appendAcDbEntity(vpId, vp);
vp->setWidth(10);
vp->setHeight(10);
vp->setCenterPoint(AcGePoint3d(5,5,0));
vp->setViewDirection(AcDb::kTopView);
vp->setViewTarget(AcGePoint3d(2.5,0,0));
vp->setViewCenter(AcGePoint2d(2.5,0));
vp->setUnlocked();
vp->setOn();
AcGeMatrix3d ms2ps(
AcDbPointRef::mswcsToPswcs(vp));
pt1 = pt1.transformBy(ms2ps);
pt2 = pt2.transformBy(ms2ps);
}
{//creates the dimension object
AcDbObjectPointer<AcDbRotatedDimension> dim;
dim.create();
dim->setXLine1Point(pt1);
dim->setXLine2Point(pt2);
dim->setDimLinePoint(AcGePoint3d(2,2,0));
ps->appendAcDbEntity(dimId, dim);
}
AcDbObjectPointer<AcDbDimAssoc> assoc;
assoc.create();
AcDbObjectIdArray arr(2);
arr.append(vpId);
arr.append(lineId);
AcDbFullSubentPath path1(
arr,
AcDbSubentId(AcDb::kVertexSubentType, 0));
AcDbFullSubentPath path2(
arr,
AcDbSubentId(AcDb::kVertexSubentType, 1));
AcGePoint3d refPt1(pt1);
AcDbOsnapPointRef *ref1 = new AcDbOsnapPointRef(
AcDbPointRef::kOsnapNear, &path1, &path1, &refPt1);
AcGePoint3d refPt2(pt2);
AcDbOsnapPointRef *ref2 = new AcDbOsnapPointRef(
AcDbPointRef::kOsnapNear, &path2, &path2, &refPt2);
assoc->setDimObjId(dimId);
assoc->setTransSpatial(true );
assoc->setPointRef(AcDbDimAssoc::kXline1Point, ref1);
assoc->setPointRef(AcDbDimAssoc::kXline2Point, ref2);
assoc->setAssocFlag(static_cast <int >(
AcDbDimAssoc::kFirstPointRef
|AcDbDimAssoc::kSecondPointRef));
assoc->updateDimension();
AcDbObjectId dimAssocId;
Acad::ErrorStatus es = acdbPostDimAssoc(
dimId, assoc, dimAssocId);
acutPrintf(_T("\\npost dim: %s\\n" ),
acadErrorStatusText(es));
if (assoc.open(dimAssocId, AcDb::kForWrite)
==Acad::eOk)
{
assoc->startCmdWatcher();
assoc->addToPointRefReactor();
assoc->addToDimensionReactor();
}
}
static void paperOrdinateDimAssoc(void )
{
AcDbDatabase* pDb
= acdbHostApplicationServices()->workingDatabase();
AcDbBlockTableRecordPointer ms(
ACDB_MODEL_SPACE, pDb, AcDb::kForWrite);
AcDbBlockTableRecordPointer ps(
ACDB_PAPER_SPACE, pDb, AcDb::kForWrite);
AcDbObjectId lineId, vpId, dimId;
AcGePoint3d pt1(AcGePoint3d::kOrigin);
AcGePoint3d pt2(5,0,0);
{//creates a line
AcDbObjectPointer<AcDbLine> line;
line.create();
line->setStartPoint(pt1);
line->setEndPoint(pt2);
ms->appendAcDbEntity(lineId, line);
}
{//creates a viewport
AcDbObjectPointer<AcDbViewport> vp;
vp.create();
ps->appendAcDbEntity(vpId, vp);
vp->setWidth(10);
vp->setHeight(10);
vp->setCenterPoint(AcGePoint3d(5,5,0));
vp->setViewDirection(AcDb::kTopView);
vp->setViewTarget(AcGePoint3d(2.5,0,0));
vp->setViewCenter(AcGePoint2d(2.5,0));
vp->setUnlocked();
vp->setOn();
AcGeMatrix3d ms2ps(
AcDbPointRef::mswcsToPswcs(vp));
pt1 = pt1.transformBy(ms2ps);
pt2 = pt2.transformBy(ms2ps);
}
{//creates the dimension object
AcDbObjectPointer<AcDbOrdinateDimension> dim;
dim.create();
dim->setUsingXAxis(true );
dim->setOrigin(pt1);
dim->setDefiningPoint(pt2);
dim->setLeaderEndPoint(pt2
+ AcGeVector3d(1.0, 0.0, 0.0));
ps->appendAcDbEntity(dimId, dim);
}
AcDbObjectPointer<AcDbDimAssoc> assoc;
assoc.create();
AcDbObjectIdArray arr(2);
arr.append(vpId);
arr.append(lineId);
AcDbFullSubentPath path1(
arr,
AcDbSubentId(AcDb::kVertexSubentType, 0));
AcDbFullSubentPath path2(
arr,
AcDbSubentId(AcDb::kVertexSubentType, 1));
AcGePoint3d refPt1(pt1);
AcDbOsnapPointRef *ref1 = new AcDbOsnapPointRef(
AcDbPointRef::kOsnapNear, &path1, &path1, &refPt1);
AcGePoint3d refPt2(pt2);
AcDbOsnapPointRef *ref2 = new AcDbOsnapPointRef(
AcDbPointRef::kOsnapNear, &path2, &path2, &refPt2);
assoc->setDimObjId(dimId);
assoc->setTransSpatial(true );
assoc->setPointRef(AcDbDimAssoc::kOriginPoint, ref1);
assoc->setPointRef(AcDbDimAssoc::kDefiningPoint, ref2);
assoc->setAssocFlag(static_cast <int >(
AcDbDimAssoc::kFirstPointRef|
AcDbDimAssoc::kSecondPointRef));
assoc->updateDimension();
AcDbObjectId dimAssocId;
Acad::ErrorStatus es =
acdbPostDimAssoc
(dimId, assoc, dimAssocId);
acutPrintf(_T("\\npost dim: %s\\n" ),
acadErrorStatusText(es));
if (assoc.open(dimAssocId, AcDb::kForWrite)==Acad::eOk)
{
assoc->startCmdWatcher();
assoc->addToPointRefReactor();
assoc->addToDimensionReactor();
}
}
Recent Comments