|
- struct PlineSegment {
- AcGePoint2d p1, p2;
- double bulge=0.0;
- int reversed=0; // 0 unknown, -1 reversed, 1 not reversed
- };
- void cmdRegion2Pline()
- {
- ads采用point pt;
- ads采用name ent;
- AcDbObjectId idRegion;
- if (acedEntSel(采用T("\nSelect region"), ent, pt) != RTNORM)
- return;
- if (acdbGetObjectId(idRegion, ent) != Acad::eOk)
- return;
- Acad::ErrorStatus es;
- AcDbVoidPtrArray curves;
- AcDbRegion *region;
- AcGePlane regionPlane;
- if ((es = acdbOpenObject(region, idRegion, AcDb::kForRead)) == Acad::eOk)
- {
- region->getPlane(regionPlane);
- region->explode(curves);
- region->close();
- }
- AcGePoint3d origin;
- AcGeVector3d axis1, axis2, norm(regionPlane.normal());
- regionPlane.getCoordSystem(origin, axis1, axis2);
- // Recalculate origin to be the closest point on the plane
- double dp = origin.asVector().dotProduct(norm);
- origin = AcGePoint3d::kOrigin + dp * norm;
- AcGeMatrix3d mat采用WCS采用ECS, mat采用ECS采用WCS;
- mat采用WCS采用ECS.setCoordSystem(origin, axis1, axis2, norm);
- mat采用ECS采用WCS = mat采用WCS采用ECS.inverse();
- int i, j, N=curves.length();
- AcDbObject *obj;
- AcDbCurve *crv;
- AcDbArc *arc;
- AcGePoint3d ps, pe;
- double angle=0.0;
- std::vector<PlineSegment> segments;
- PlineSegment segment;
- // Collect segment data:
- for (i=0; i<N; ++i)
- {
- obj = (AcDbObject*)curves[i];
- if (crv = AcDbCurve::cast(obj))
- {
- crv->getStartPoint(ps);
- crv->getEndPoint(pe);
- // Transform to ECS. After this z-components should be 0
- ps.transformBy(mat采用ECS采用WCS);
- pe.transformBy(mat采用ECS采用WCS);
- segment.p1 = AcGePoint2d(ps.x, ps.y);
- segment.p2 = AcGePoint2d(pe.x, pe.y);
- if (arc = AcDbArc::cast(obj))
- segment.bulge = tan(0.25 * arc->totalAngle());
- else
- segment.bulge = 0.0;
- segments.push采用back(segment);
- }
- delete obj;
- }
- // When we stitch the polyline together we must make sure that start- and endpoints match.
- AcDbDatabase *pDB = acdbHostApplicationServices()->workingDatabase();
- AcDbPolyline* pline = new AcDbPolyline();
- pline->setDatabaseDefaults(pDB);
- unsigned int ix = 0;
- for (i = 0; i < N; ++i) {
- j = (i>0)? i-1 : N-1;
- PlineSegment &ps = segments[i];
- PlineSegment& psPrev = segments[j];
- if (psPrev.p2 == ps.p1) {
- psPrev.reversed = ps.reversed = 1;
- pline->addVertexAt(ix++, ps.p1, ps.reversed * ps.bulge);
- } else if (psPrev.p1 == ps.p1){
- ps.reversed = 1;
- psPrev.reversed = -1;
- pline->addVertexAt(ix++, ps.p1, ps.reversed * ps.bulge);
- } else if (psPrev.p2 == ps.p2) {
- ps.reversed = -1;
- psPrev.reversed = 1;
- pline->addVertexAt(ix++, ps.p2, ps.reversed * ps.bulge);
- } else if (psPrev.p1 == ps.p2) {
- psPrev.reversed = ps.reversed = -1;
- pline->addVertexAt(ix++, ps.p2, ps.reversed * ps.bulge);
- }
- }
- pline->setClosed(true);
- pline->transformBy(mat采用WCS采用ECS);
- PostToDb(pDB, pline);
- }
复制代码 |
|