<?xml encoding=”UTF-8″>By Balaji Ramamoorthy
Most of the code in this blog post is from a code snippet that my colleague Philippe Leefsma implemented. While his original code created an aligned dimension in paperspace, i have modified it slightly to create an ordinate dimension to cater to a recent developer request.
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.
<span>//For AcDbDimAssoc</span><span> </span>
<span>#include</span><span> <span>"dbdimassoc.h"</span><span> </span></span>
<span>//For AcDbOsnapPointRef</span><span> </span>
<span>#include</span><span> <span>"dbdimptref.h"</span><span> </span></span>
<span>static</span><span> <span>void</span><span> paperRotatedDimAssoc(<span>void</span><span> )</span></span></span>
<span>{</span>
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);
<span>{</span><span>//creates a line</span><span> </span>
AcDbObjectPointer<AcDbLine> line;
line.create();
line->setStartPoint(pt1);
line->setEndPoint(pt2);
ms->appendAcDbEntity(lineId, line);
<span>}</span>
<span>{</span><span>//creates a viewport</span><span> </span>
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);
<span>}</span>
<span>{</span><span>//creates the dimension object</span><span> </span>
AcDbObjectPointer<AcDbRotatedDimension> dim;
dim.create();
dim->setXLine1Point(pt1);
dim->setXLine2Point(pt2);
dim->setDimLinePoint(AcGePoint3d(2,2,0));
ps->appendAcDbEntity(dimId, dim);
<span>}</span>
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 = <span>new</span><span> AcDbOsnapPointRef(</span>
AcDbPointRef::kOsnapNear, &path1, &path1, &refPt1);
AcGePoint3d refPt2(pt2);
AcDbOsnapPointRef *ref2 = <span>new</span><span> AcDbOsnapPointRef(</span>
AcDbPointRef::kOsnapNear, &path2, &path2, &refPt2);
assoc->setDimObjId(dimId);
assoc->setTransSpatial(<span>true</span><span> );</span>
assoc->setPointRef(AcDbDimAssoc::kXline1Point, ref1);
assoc->setPointRef(AcDbDimAssoc::kXline2Point, ref2);
assoc->setAssocFlag(<span>static_cast</span><span> <<span>int</span><span> >(</span></span>
AcDbDimAssoc::kFirstPointRef
|AcDbDimAssoc::kSecondPointRef));
assoc->updateDimension();
AcDbObjectId dimAssocId;
Acad::ErrorStatus es = acdbPostDimAssoc(
dimId, assoc, dimAssocId);
acutPrintf(_T(<span>"\npost dim: %s\n"</span><span> ), </span>
acadErrorStatusText(es));
<span>if</span><span> (assoc.open(dimAssocId, AcDb::kForWrite)</span>
==Acad::eOk)
<span>{</span>
assoc->startCmdWatcher();
assoc->addToPointRefReactor();
assoc->addToDimensionReactor();
<span>}</span>
<span>}</span>
<span>static</span><span> <span>void</span><span> paperOrdinateDimAssoc(<span>void</span><span> )</span></span></span>
<span>{</span>
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);
<span>{</span><span>//creates a line</span><span> </span>
AcDbObjectPointer<AcDbLine> line;
line.create();
line->setStartPoint(pt1);
line->setEndPoint(pt2);
ms->appendAcDbEntity(lineId, line);
<span>}</span>
<span>{</span><span>//creates a viewport</span><span> </span>
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);
<span>}</span>
<span>{</span><span>//creates the dimension object</span><span> </span>
AcDbObjectPointer<AcDbOrdinateDimension> dim;
dim.create();
dim->setUsingXAxis(<span>true</span><span> );</span>
dim->setOrigin(pt1);
dim->setDefiningPoint(pt2);
dim->setLeaderEndPoint(pt2
+ AcGeVector3d(1.0, 0.0, 0.0));
ps->appendAcDbEntity(dimId, dim);
<span>}</span>
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 = <span>new</span><span> AcDbOsnapPointRef(</span>
AcDbPointRef::kOsnapNear, &path1, &path1, &refPt1);
AcGePoint3d refPt2(pt2);
AcDbOsnapPointRef *ref2 = <span>new</span><span> AcDbOsnapPointRef(</span>
AcDbPointRef::kOsnapNear, &path2, &path2, &refPt2);
assoc->setDimObjId(dimId);
assoc->setTransSpatial(<span>true</span><span> );</span>
assoc->setPointRef(AcDbDimAssoc::kOriginPoint, ref1);
assoc->setPointRef(AcDbDimAssoc::kDefiningPoint, ref2);
assoc->setAssocFlag(<span>static_cast</span><span> <<span>int</span><span> >(</span></span>
AcDbDimAssoc::kFirstPointRef|
AcDbDimAssoc::kSecondPointRef));
assoc->updateDimension();
AcDbObjectId dimAssocId;
Acad::ErrorStatus es =
acdbPostDimAssoc
(dimId, assoc, dimAssocId);
acutPrintf(_T(<span>"\npost dim: %s\n"</span><span> ), </span>
acadErrorStatusText(es));
<span>if</span><span> (assoc.open(dimAssocId, AcDb::kForWrite)==Acad::eOk)</span>
<span>{</span>
assoc->startCmdWatcher();
assoc->addToPointRefReactor();
assoc->addToDimensionReactor();
<span>}</span>
<span>}</span>

Leave a Reply to BalajiCancel reply