/////////////////////////////////////////////////////////////////////////////// // Copyright (C) 2002-2025, Open Design Alliance (the "Alliance"). // All rights reserved. // // This software and its documentation and related materials are owned by // the Alliance. The software may only be incorporated into application // programs owned by members of the Alliance, subject to a signed // Membership Agreement and Supplemental Software License Agreement with the // Alliance. The structure and organization of this software are the valuable // trade secrets of the Alliance and its suppliers. The software is also // protected by copyright law and international treaty provisions. Application // programs incorporating this software must include the following statement // with their copyright notices: // // This application incorporates Open Design Alliance software pursuant to a license // agreement with Open Design Alliance. // Open Design Alliance Copyright (C) 2002-2025 by Open Design Alliance. // All rights reserved. // // By use of this software, its documentation or related materials, you // acknowledge and accept the above terms. /////////////////////////////////////////////////////////////////////////////// // ExLine.cpp: implementation of the ExLine function. // ////////////////////////////////////////////////////////////////////// #include "StdAfx.h" #include "DbPolyline.h" #include "../ExCustObjs/ClassFilter.h" #include "Ge/GeClipBoundary2d.h" #include "Ge/GePolyline3d.h" #include "Ge/GeCompositeCurve3d.h" void getPoly(OdSmartPtr &pIO, OdString str, OdGePoint2dArray &points2d) { ClassFilter filter(OdDbPolyline::desc()); OdDbSelectionSetPtr pSSet = pIO->select(str.c_str(), OdEd::kSelSingleEntity | OdEd::kSelSinglePass, 0, OdString::kEmpty, &filter); if (pSSet->numEntities() != 1) return; OdDbSelectionSetIteratorPtr pIter = pSSet->newIterator(); OdDbObjectId objId = pIter->objectId(); OdDbPolylinePtr pPolyToClip = objId.openObject(OdDb::kForRead); if (!pPolyToClip->isOnlyLines()) { pIO->putString("Polyline must consist of line segments only."); return; } OdGeCurve3d* pGeCurve = 0; pPolyToClip->getOdGeCurve(pGeCurve); OdGePoint3dArray points3d; pGeCurve->getSamplePoints(0, 0., points3d); delete pGeCurve; if (points3d.size() < 2) { pIO->putString("Failed to collect geometry information."); return; } points2d.reserve(points3d.size()); for (auto p : points3d) points2d.append(p.convert2d()); } void _ClipBoundary_func(OdEdCommandContext* pCmdCtx) { // Limitation of this command: both curves must have Z=0! OdDbCommandContextPtr pDbCmdCtx(pCmdCtx); OdSmartPtr pIO = pDbCmdCtx->userIO(); OdDbDatabasePtr pDb = pDbCmdCtx->database(); // Select polyline (open or closed) to clip // Closed polyline = polygon mode OdGePoint2dArray clipMe2d; getPoly(pIO, "Select open or closed polyline to clip:", clipMe2d); // Select closed clipping polyline OdGePoint2dArray clipByMe2d; getPoly(pIO, "Select closed clipping polyline:", clipByMe2d); // Proceed the clip operation bool bPolygonMode = clipMe2d.first().isEqualTo(clipMe2d.last()); OdGeClipBoundary2d clipBoundary; OdGe::ClipError clipError = clipBoundary.set(clipByMe2d); if (OdGe::eOk != clipError) { pIO->putString("Bad clip contour."); return; } OdGe::ClipCondition clipCondition; OdGePoint2dArray clippedContour2d; if (bPolygonMode) { pIO->putString("clipPolygon mode."); clipError = clipBoundary.clipPolygon(clipMe2d, clippedContour2d, clipCondition); } else { pIO->putString("clipPolyline mode."); clipError = clipBoundary.clipPolyline(clipMe2d, clippedContour2d, clipCondition); } if (OdGe::eOk != clipError) { pIO->putString("Clipping failed."); return; } pIO->putString("Clipping succeeded."); // Create new polyline and add it to active space OdDbObjectId spaceId = ExCommandsUtils::activeBlockTableRecord(pDb); OdDbBlockTableRecordPtr pSpace = spaceId.safeOpenObject(OdDb::kForWrite); OdDbPolylinePtr pNewPoly = OdDbPolyline::createObject(); pNewPoly->setDatabaseDefaults(pDb); pNewPoly->setColorIndex(bPolygonMode ? 1 : 3); OdGePoint3dArray clippedContour3d; clippedContour3d.reserve(clippedContour2d.size()); for (auto p : clippedContour2d) clippedContour3d.append(OdGePoint3d(p.x, p.y, 0)); OdGeCurve3dPtrArray list; list.append(new OdGePolyline3d(clippedContour3d)); OdGeCompositeCurve3d composite(list); if (eOk != pNewPoly->setFromOdGeCurve(composite)) { pIO->putString("Failed to create new polyline entity."); return; } pSpace->appendOdDbEntity(pNewPoly); }