ENDPT_410_endpoint_open

本文介绍了如何初始化用户层的收包队列和事件队列,包括设置队列头尾指针、长度,并创建信号量及互斥锁。
//为用户层准备,初始化收包队列
packetQueueInit();
   packetQueue.head     = 0;
   packetQueue.tail     = 0;
   packetQueue.length   = 0;
   
   memset( &packetAvailableSemaphore, 0, sizeof(packetAvailableSemaphore) );
   bosSemCreate( "PBLK", BOS_CFG_SEM_INIT_COUNT, BOS_CFG_SEM_MAX_COUNT, &packetAvailableSemaphore );

//为用户层准备,初始化接收到事件队列
eventQueueInit();
   eventQueue.head   = 0;
   eventQueue.tail   = 0;
   eventQueue.length = 0;
   
   bosMutexCreate( "EVTQ", &eventQueueMutex );
   memset( &eventAvailableSemaphore, 0, sizeof(eventAvailableSemaphore) );
   bosSemCreate( "EVBL", BOS_CFG_SEM_INIT_COUNT, BOS_CFG_SEM_MAX_COUNT, &eventAvailableSemaphore );


分析这段代码if (iterr->first.Find(L"桥架") > -1) { dmtl.extractCurveInfos(iterr->second, vCurves, nCountOfCurves, false); vector<DwgElecCableTray> m_CableTray; vector<DwgElecCableTrayElbow> mCableTrayElbow; vector<DwgElecCableTray3T> mCableTrayElbow3T; vector<DwgElecCableTray4T> mCableTrayElbow4T; //用于寻找连接关系的所有线段的集合 vector<geoSegLine> vAllSegLines; //找到当前集合中所有与设备相连接的线段 vector<qCadDbObjectPtr> vObjs; for (auto id : iterr->second) { qCadDbObjectPtr obj = id.openObject(); vObjs.push_back(obj); } //排除不属于判断标准的管线 map<int, std::vector<std::pair<qCadDbObjectId, geoCurve*>>> mTraysValid, mTraysInValid; int nCountValid = 0, nCountInValid = 0; for (auto x : vCurves) { std::vector<std::pair<qCadDbObjectId, geoCurve*>> vTemp; vTemp = searchAllConnectedLineWW(x.second, x.first, vCurves); if (vTemp.size() > 2) { mTraysValid[nCountValid++] = vTemp; } else { mTraysInValid[nCountInValid] = vTemp; } } vector<pair<qCadDbObjectId, geoSegLine>> vCableTrays; map<qCadDbObjectId, geoSegLine> mCableTrays; for (auto iterLine = mTraysValid.begin(); iterLine != mTraysValid.end(); iterLine++) { vector<pair<qCadDbObjectId, geoCurve *>> vCT; vCT = iterLine->second; for (auto temp : vCT) { qCadDbObjectPtr pObj = temp.first.openObject(); qCadDbObjectId pId = temp.first; if (!pObj) continue; qCadDbEntityPtr pEnt = qCadDbEntity::cast(pObj); if (!pEnt) continue; if (pEnt->visibility() == qCadDb::qInvisible) continue; //折线数据提取 if (pObj->isKindOf(qCadDbPolyline::desc())) { geoSegLine tempLine; DwgElecConduit conduit; qCadDbPolylinePtr pBlkPloy = qCadDbPolyline::cast(pObj); geoPoint3d firstP, lastP; int size = pBlkPloy->numVerts(); pBlkPloy->getPointAt(0, firstP); pBlkPloy->getPointAt(size - 1, lastP); tempLine.Set(firstP, lastP); tempLine.setState(0); vCableTrays.push_back(make_pair(pId, tempLine)); mCableTrays[pId] = tempLine; } //直线数据提取 else if (pObj->isKindOf(qCadDbLine::desc())) { geoSegLine tempLine; DwgElecConduit conduitForLine; qCadDbLinePtr pBlkLine = qCadDbLine::cast(pObj); geoPoint3d firstP, lastP; pBlkLine->getStartPoint(firstP); pBlkLine->getEndPoint(lastP); tempLine.Set(firstP, lastP); tempLine.setState(0); vCableTrays.push_back(make_pair(pId, tempLine)); mCableTrays[pId] = tempLine; } //弧线数据提取 else if (pObj->isKindOf(qCadDbArc::desc())) { geoSegLine tempLine; DwgElecConduit conduitArc; qCadDbArcPtr pBlkArc = qCadDbArc::cast(pObj); geoPoint3d firstP, lastP; pBlkArc->getStartPoint(firstP); pBlkArc->getEndPoint(lastP); tempLine.Set(firstP, lastP); tempLine.setState(0); vCableTrays.push_back(make_pair(pId, tempLine)); mCableTrays[pId] = tempLine; } } } //分析所有的双线桥架对应关系 for (auto lineI : vCableTrays) { //判断两条线是否平行与是否为最近距离线段 vector<pair<double, qCadDbObjectId>> vElecData; for (auto lineII : vCableTrays) { if (lineI.second.IsParallelTo(lineII.second) && lineI.second.lenth() >= lineII.second.lenth() - 300 && lineI.second.lenth() <= lineII.second.lenth() + 300) { geoPoint3d startP, endP, startPerp, endPerp; double startDistt = 0, endDistt = 0; startP = lineI.second.startPoint(); endP = lineI.second.endPoint(); lineII.second.GetPerpPt(startP, startPerp); lineII.second.GetPerpPt(endP, endPerp); startDistt = startP.DistanceTo(startPerp); endDistt = endP.DistanceTo(endPerp); //确认垂足数量 0 为没有交集 if (startDistt > endDistt && endDistt != 0) { for (auto num : m_CableTraySize) { if ((abs(num - endDistt) < 1) && (abs(lineI.second.lenth() - lineII.second.lenth()) < 10)) { vElecData.push_back(make_pair(endDistt, lineII.first)); } } } else if (startDistt <= endDistt && startDistt != 0) { for (auto num : m_CableTraySize) { if ((abs(num - startDistt) < 1) && (abs(lineI.second.lenth() - lineII.second.lenth()) < 10)) { vElecData.push_back(make_pair(startDistt, lineII.first)); } } } } } if (vElecData.size() == 0) continue; sort(vElecData.begin(), vElecData.end(), FindSmallestNum); DwgElecCableTray tray; tray.m_CableTray1ID = lineI.first; tray.m_CableTrayLine1 = lineI.second; tray.m_CableTray2ID = vElecData[0].second; tray.m_CableTrayLine2 = mCableTrays[vElecData[0].second]; tray.m_CableTrayRadius = vElecData[0].first / 2; tray.m_systemName = iterr->first; m_CableTray.push_back(tray); } //删除重复的数据 vector<DwgElecCableTray> vStoredData; for (int i = 0; i < m_CableTray.size(); i++) { bool passOrNot = true; if (!vStoredData.empty()) { for (int j = 0; j < vStoredData.size(); j++) { CString id1LineOne, id1LineTwo, id2LineOne, id2LineTwo; id1LineOne = GetDuctLineId(m_CableTray[i].m_CableTray1ID); id1LineTwo = GetDuctLineId(m_CableTray[i].m_CableTray2ID); id2LineOne = GetDuctLineId(vStoredData[j].m_CableTray1ID); id2LineTwo = GetDuctLineId(vStoredData[j].m_CableTray2ID); if ((id1LineOne == id2LineOne && id1LineTwo == id2LineTwo) || (id1LineOne == id2LineTwo && id1LineTwo == id2LineOne) && m_CableTray[i].m_CableTrayCenterLine.lenth() == vStoredData[j].m_CableTrayCenterLine.lenth()) { passOrNot = false; break; } } } if (passOrNot == true) { vStoredData.push_back(m_CableTray[i]); } } if (!vStoredData.empty()) { m_CableTray.clear(); m_CableTray = vStoredData; } vStoredData.clear(); //找到符合模数和长度的桥架 for (auto iterC = m_CableTray.begin(); iterC != m_CableTray.end(); iterC++) { geoPoint3d perPt1, perPt2, perPt3, perPt4; vector<pair<geoPoint3d, geoPoint3d>> perPoints; vector<geoPoint3d> pointsOnLine; //找到两条线的每一条垂足 if (iterC->m_CableTrayLine2.GetPerpPt(iterC->m_CableTrayLine1.startPoint(), perPt1) == 0) { perPoints.push_back(make_pair(perPt1, iterC->m_CableTrayLine1.startPoint())); } if (iterC->m_CableTrayLine2.GetPerpPt(iterC->m_CableTrayLine1.endPoint(), perPt2) == 0) { perPoints.push_back(make_pair(perPt2, iterC->m_CableTrayLine1.endPoint())); } if (iterC->m_CableTrayLine1.GetPerpPt(iterC->m_CableTrayLine2.startPoint(), perPt3) == 0) { perPoints.push_back(make_pair(perPt3, iterC->m_CableTrayLine2.startPoint())); } if (iterC->m_CableTrayLine1.GetPerpPt(iterC->m_CableTrayLine2.endPoint(), perPt4) == 0) { perPoints.push_back(make_pair(perPt4, iterC->m_CableTrayLine2.endPoint())); } if (perPoints.size() == 4) { geoPoint3d startPt, endPt; geoSegLine centerLine; double distanceOfTwoLines; if (iterC->m_CableTrayLine1.startPoint().DistanceTo(iterC->m_CableTrayLine2.startPoint()) > iterC->m_CableTrayLine1.startPoint().DistanceTo(iterC->m_CableTrayLine2.endPoint())) { startPt.x = (iterC->m_CableTrayLine1.startPoint().x + iterC->m_CableTrayLine2.endPoint().x) / 2; startPt.y = (iterC->m_CableTrayLine1.startPoint().y + iterC->m_CableTrayLine2.endPoint().y) / 2; endPt.x = (iterC->m_CableTrayLine1.endPoint().x + iterC->m_CableTrayLine2.startPoint().x) / 2; endPt.y = (iterC->m_CableTrayLine1.endPoint().y + iterC->m_CableTrayLine2.startPoint().y) / 2; distanceOfTwoLines = iterC->m_CableTrayLine1.startPoint().DistanceTo(iterC->m_CableTrayLine2.endPoint()); } else if (iterC->m_CableTrayLine1.startPoint().DistanceTo(iterC->m_CableTrayLine2.startPoint()) < iterC->m_CableTrayLine1.startPoint().DistanceTo(iterC->m_CableTrayLine2.endPoint())) { startPt.x = (iterC->m_CableTrayLine1.startPoint().x + iterC->m_CableTrayLine2.startPoint().x) / 2; startPt.y = (iterC->m_CableTrayLine1.startPoint().y + iterC->m_CableTrayLine2.startPoint().y) / 2; endPt.x = (iterC->m_CableTrayLine1.endPoint().x + iterC->m_CableTrayLine2.endPoint().x) / 2; endPt.y = (iterC->m_CableTrayLine1.endPoint().y + iterC->m_CableTrayLine2.endPoint().y) / 2; distanceOfTwoLines = iterC->m_CableTrayLine1.startPoint().DistanceTo(iterC->m_CableTrayLine2.startPoint()); } centerLine.Set(startPt, endPt); //if (distanceOfTwoLines > iterC->m_CableTrayLine1.lenth()) //{ // continue; //} iterC->m_CableTrayCenterLine = centerLine; iterC->m_CableTrayId = GetDuctLineId(iterC->m_CableTray1ID) + L"#" + GetDuctLineId(iterC->m_CableTray2ID); vStoredData.push_back(*iterC); } } // m_CableTray.clear(); // m_CableTray = vStoredData; // vStoredData.clear(); // //查找所有复合模数但不是桥架的线配对 // for (auto nonCableTray : m_CableTray) // { // if (nonCableTray.m_CableTrayLine1.startPoint().DistanceTo(nonCableTray.m_CableTrayLine2.startPoint()) <= nonCableTray.m_CableTrueRange.lenth()) // { // vStoredData.push_back(nonCableTray); // } // } m_CableTray.clear(); m_CableTray = vStoredData; vStoredData.clear(); //在所有选中线中寻找符合弯头的形状 并将所有弯头编号 map<int, DwgElecCableTrayElbow> vElbows; int nCount = 0; for (auto line : vCableTrays) { DwgElecCableTrayElbow elbow; vector<pair<geoPoint3d, pair<qCadDbObjectId, geoSegLine>>> vStartTray; vector<pair<geoPoint3d, pair<qCadDbObjectId, geoSegLine>>> vEndTray; for (auto lineConect : vCableTrays) { if (lineConect.first != line.first) { if (line.second.startPoint().DistanceTo(lineConect.second.startPoint()) < 10) { geoVector2d v1(lineConect.second.endPoint().x - lineConect.second.startPoint().x, lineConect.second.endPoint().y - lineConect.second.startPoint().y); geoVector2d v2(line.second.endPoint().x - line.second.startPoint().x, line.second.endPoint().y - line.second.startPoint().y); if (LineIsPerpAt(v1, v2)) { vStartTray.push_back(make_pair(lineConect.second.endPoint(), lineConect)); } } if (line.second.startPoint().DistanceTo(lineConect.second.endPoint()) < 10) { geoVector2d v1(lineConect.second.startPoint().x - lineConect.second.endPoint().x, lineConect.second.startPoint().y - lineConect.second.endPoint().y); geoVector2d v2(line.second.endPoint().x - line.second.startPoint().x, line.second.endPoint().y - line.second.startPoint().y); if (LineIsPerpAt(v1, v2)) { vStartTray.push_back(make_pair(lineConect.second.startPoint(), lineConect)); } } if (line.second.endPoint().DistanceTo(lineConect.second.startPoint()) < 10) { geoVector2d v1(lineConect.second.endPoint().x - lineConect.second.startPoint().x, lineConect.second.endPoint().y - lineConect.second.startPoint().y); geoVector2d v2(line.second.startPoint().x - line.second.endPoint().x, line.second.startPoint().y - line.second.endPoint().y); if (LineIsPerpAt(v1, v2)) { vEndTray.push_back(make_pair(lineConect.second.endPoint(), lineConect)); } } if (line.second.endPoint().DistanceTo(lineConect.second.endPoint()) < 10) { geoVector2d v1(lineConect.second.startPoint().x - lineConect.second.endPoint().x, lineConect.second.startPoint().y - lineConect.second.endPoint().y); geoVector2d v2(line.second.startPoint().x - line.second.endPoint().x, line.second.startPoint().y - line.second.endPoint().y); if (LineIsPerpAt(v1, v2)) { vEndTray.push_back(make_pair(lineConect.second.startPoint(), lineConect)); } } } } if (vStartTray.size() >= 1 && vEndTray.size() >= 1) { elbow.connectLine1 = vStartTray[0].second.first; elbow.connectLine2 = vEndTray[0].second.first; elbow.startLine = vStartTray[0].second.second; elbow.startPoint = vStartTray[0].first; elbow.endLine = vEndTray[0].second.second; elbow.endPoint = vEndTray[0].first; vElbows[nCount++] = elbow; //测试弯头识别是否成功 //qCadDbLinePtr pLine = qCadDbLine::createObject(); //pLine->setStartPoint(elbow.startPoint); //pLine->setEndPoint(elbow.endPoint); //qCadCmColor color; //color.setRGB(225, 107, 162); //水粉色 //pLine->setColorIndex(6); //洋红色 //qCadDbObjectId objId; //objId = qCadInterfaceMgr::CreateNewEntity(pLine); } } //排除桥架与弯头的重叠处(一小段弯头被识别为桥架) vector<DwgElecCableTray> vNewCableTray; for (auto duplicateTray : m_CableTray) { bool bDuplicateTray = false; for (auto dElbow : vElbows) { if (duplicateTray.m_CableTrayLine1.startPoint().DistanceTo(dElbow.second.startLine.startPoint()) < 10 && duplicateTray.m_CableTrayLine1.endPoint().DistanceTo(dElbow.second.startLine.endPoint()) < 10) { bDuplicateTray = true; } else if (duplicateTray.m_CableTrayLine1.startPoint().DistanceTo(dElbow.second.startLine.endPoint()) < 10 && duplicateTray.m_CableTrayLine1.endPoint().DistanceTo(dElbow.second.startLine.startPoint()) < 10) { bDuplicateTray = true; } else if (duplicateTray.m_CableTrayLine1.startPoint().DistanceTo(dElbow.second.endLine.startPoint()) < 10 && duplicateTray.m_CableTrayLine1.endPoint().DistanceTo(dElbow.second.endLine.endPoint()) < 10) { bDuplicateTray = true; } else if (duplicateTray.m_CableTrayLine1.startPoint().DistanceTo(dElbow.second.endLine.endPoint()) < 10 && duplicateTray.m_CableTrayLine1.endPoint().DistanceTo(dElbow.second.endLine.startPoint()) < 10) { bDuplicateTray = true; } else if (duplicateTray.m_CableTrayLine2.startPoint().DistanceTo(dElbow.second.startLine.startPoint()) < 10 && duplicateTray.m_CableTrayLine2.endPoint().DistanceTo(dElbow.second.startLine.endPoint()) < 10) { bDuplicateTray = true; } else if (duplicateTray.m_CableTrayLine2.startPoint().DistanceTo(dElbow.second.startLine.endPoint()) < 10 && duplicateTray.m_CableTrayLine2.endPoint().DistanceTo(dElbow.second.startLine.startPoint()) < 10) { bDuplicateTray = true; } else if (duplicateTray.m_CableTrayLine2.startPoint().DistanceTo(dElbow.second.endLine.startPoint()) < 10 && duplicateTray.m_CableTrayLine2.endPoint().DistanceTo(dElbow.second.endLine.endPoint()) < 10) { bDuplicateTray = true; } else if (duplicateTray.m_CableTrayLine2.startPoint().DistanceTo(dElbow.second.endLine.endPoint()) < 10 && duplicateTray.m_CableTrayLine2.endPoint().DistanceTo(dElbow.second.endLine.startPoint()) < 10) { bDuplicateTray = true; } } if (!bDuplicateTray) { vNewCableTray.push_back(duplicateTray); } } m_CableTray.clear(); m_CableTray = vNewCableTray; //生成写出id for (auto ids : m_CableTray) { ids.m_CableTrayId = GetDuctLineId(ids.m_CableTray1ID) + L"#" + GetDuctLineId(ids.m_CableTray2ID); DwgElecCableTray tempId; tempId.m_CableTrayCenterLine = ids.m_CableTrayCenterLine; tempId.m_CableTrayId = ids.m_CableTrayId; tempId.m_CableTrayRadius = ids.m_CableTrayRadius; tempId.m_systemName = ids.m_systemName; mElecDataSet[iter->first].m_ElecCableTrays.push_back(tempId); } //寻找变径 //寻找两根符合要求的线 vector<std::pair<geoSegLine, geoSegLine>> vFitLines; for (auto temp : vCableTrays) { for (auto line : vCableTrays) { //不是同一条线且不平行 if (temp.first != line.first && !temp.second.IsParallelTo(line.second) && temp.second.AngleTo(line.second) > 0 && temp.second.AngleTo(line.second) < qCad_PI / 2.0 && !temp.second.IsInersectWith2d(line.second, 10)) { vFitLines.push_back(make_pair(temp.second, line.second)); } } } //寻找与这两条线相连接的桥架 vector<DwgElecCableTrayReducing> vCTReducings; //for (auto CT1 : m_CableTray) //{ // for (auto CT2 : m_CableTray) // { // if (CT1.second.startPoint().DistanceTo(matchCT.m_CableTrayLine1.startPoint()) < 25) // { // } // else if (CT1.second.startPoint().DistanceTo(matchCT.m_CableTrayLine1.endPoint()) < 25) // { // } // if (CT1.second.endPoint().DistanceTo(matchCT.m_CableTrayLine1.startPoint()) < 25) // { // } // else if (CT1.second.endPoint().DistanceTo(matchCT.m_CableTrayLine1.endPoint()) < 25) // { // } // } //} //通过弯头找到相连接的桥架 //判断弯头所在构件的完整类型 1 弯头 2 三通 3 四通 for (auto iterE = vElbows.begin(); iterE != vElbows.end(); iterE++) { if (iterE->second.ischecked) continue; vector<DwgElecCableTrayElbow> cableTrayCount; cableTrayCount.push_back(iterE->second); iterE->second.ischecked = true; DwgElecCableTray cableTrayConnect1, cableTrayConnect2; vector<pair<double, DwgElecCableTray>> vClosetCableTrayS, vClosetCableTrayE; vector<DwgElecCableTray> vClosetCableTS, vClosetCableTE; for (auto centerLine : m_CableTray) { //与弯头的起点线平行 if (centerLine.m_CableTrayCenterLine.IsParallelTo(iterE->second.startLine)) { geoPoint3d intersectP; if (centerLine.m_CableTrayLine1.IntersectWith(iterE->second.startLine, intersectP, 20)) { vClosetCableTS.push_back(centerLine); } else if (centerLine.m_CableTrayLine2.IntersectWith(iterE->second.startLine, intersectP, 20)) { vClosetCableTS.push_back(centerLine); } double distS = centerLine.m_CableTrayCenterLine.startPoint().DistanceTo(iterE->second.startPoint); double distE = centerLine.m_CableTrayCenterLine.endPoint().DistanceTo(iterE->second.startPoint); if (distS > distE && distE <= 400) { vClosetCableTrayS.push_back(make_pair(distE, centerLine)); } else if (distS < distE && distS <= 400) { vClosetCableTrayS.push_back(make_pair(distS, centerLine)); } } //与弯头终点线平行 if (centerLine.m_CableTrayCenterLine.IsParallelWith(iterE->second.endLine)) { geoPoint3d intersectP; if (centerLine.m_CableTrayLine1.IntersectWith(iterE->second.endLine, intersectP, 20)) { vClosetCableTE.push_back(centerLine); } else if (centerLine.m_CableTrayLine2.IntersectWith(iterE->second.endLine, intersectP, 20)) { vClosetCableTE.push_back(centerLine); } double distS = centerLine.m_CableTrayCenterLine.startPoint().DistanceTo(iterE->second.endPoint); double distE = centerLine.m_CableTrayCenterLine.endPoint().DistanceTo(iterE->second.endPoint); if (distS > distE && distE <= 400) { vClosetCableTrayE.push_back(make_pair(distE, centerLine)); } else if (distS < distE && distS <= 400) { vClosetCableTrayE.push_back(make_pair(distS, centerLine)); } } } if (vClosetCableTrayS.size() > 0) { sort(vClosetCableTrayS.begin(), vClosetCableTrayS.end(), FindClosetNum); cableTrayConnect1 = vClosetCableTrayS[0].second; } if (vClosetCableTrayE.size() > 0) { sort(vClosetCableTrayE.begin(), vClosetCableTrayE.end(), FindClosetNum); cableTrayConnect2 = vClosetCableTrayE[0].second; } //找到桥架另一端是否还有其他弯头 vector<pair<double, DwgElecCableTrayElbow>> vClosetCableTrayElow1, vClosetCableTrayElow2; for (auto iterF = vElbows.begin(); iterF != vElbows.end(); iterF++) { if (iterF->first != iterE->first && iterF->second.ischecked == false) { if (iterF->second.startLine.IsParallelWith(cableTrayConnect1.m_CableTrayCenterLine)) { double dist = iterF->second.startPoint.DistanceTo(cableTrayConnect1.m_CableTrayCenterLine.startPoint()); double distOther = iterF->second.startPoint.DistanceTo(cableTrayConnect1.m_CableTrayCenterLine.endPoint()); double distElbows1 = iterF->second.startPoint.DistanceTo(iterE->second.startPoint); double distElbows2 = iterF->second.startPoint.DistanceTo(iterE->second.endPoint); if (dist < 400 && (distElbows1 <= cableTrayConnect1.m_CableTrayRadius * 2 || distElbows2 <= cableTrayConnect1.m_CableTrayRadius * 2)) { iterF->second.ischecked = true; vClosetCableTrayElow1.push_back(make_pair(dist, iterF->second)); } else if (distOther < 400 && (distElbows1 <= cableTrayConnect1.m_CableTrayRadius * 2 || distElbows2 <= cableTrayConnect1.m_CableTrayRadius * 2)) { iterF->second.ischecked = true; vClosetCableTrayElow1.push_back(make_pair(distOther, iterF->second)); } } if (iterF->second.endLine.IsParallelWith(cableTrayConnect1.m_CableTrayCenterLine)) { double dist = iterF->second.endPoint.DistanceTo(cableTrayConnect1.m_CableTrayCenterLine.startPoint()); double distOther = iterF->second.endPoint.DistanceTo(cableTrayConnect1.m_CableTrayCenterLine.endPoint()); double distElbows1 = iterF->second.endPoint.DistanceTo(iterE->second.startPoint); double distElbows2 = iterF->second.endPoint.DistanceTo(iterE->second.endPoint); if (dist < 400 && (distElbows1 <= cableTrayConnect1.m_CableTrayRadius * 2 || distElbows2 <= cableTrayConnect1.m_CableTrayRadius * 2)) { iterF->second.ischecked = true; vClosetCableTrayElow1.push_back(make_pair(dist, iterF->second)); } else if (distOther < 400 && (distElbows1 <= cableTrayConnect1.m_CableTrayRadius * 2 || distElbows2 <= cableTrayConnect1.m_CableTrayRadius * 2)) { iterF->second.ischecked = true; vClosetCableTrayElow1.push_back(make_pair(distOther, iterF->second)); } } if (iterF->second.startLine.IsParallelWith(cableTrayConnect2.m_CableTrayCenterLine)) { double dist = iterF->second.startPoint.DistanceTo(cableTrayConnect2.m_CableTrayCenterLine.startPoint()); double distOther = iterF->second.startPoint.DistanceTo(cableTrayConnect2.m_CableTrayCenterLine.endPoint()); double distElbows1 = iterF->second.startPoint.DistanceTo(iterE->second.startPoint); double distElbows2 = iterF->second.startPoint.DistanceTo(iterE->second.endPoint); if (dist < 400 && (distElbows1 <= cableTrayConnect2.m_CableTrayRadius * 2 || distElbows2 <= cableTrayConnect2.m_CableTrayRadius * 2)) { iterF->second.ischecked = true; vClosetCableTrayElow2.push_back(make_pair(dist, iterF->second)); } else if (distOther < 400 && (distElbows1 <= cableTrayConnect1.m_CableTrayRadius * 2 || distElbows2 <= cableTrayConnect1.m_CableTrayRadius * 2)) { iterF->second.ischecked = true; vClosetCableTrayElow1.push_back(make_pair(distOther, iterF->second)); } } if (iterF->second.endLine.IsParallelWith(cableTrayConnect2.m_CableTrayCenterLine)) { double dist = iterF->second.endPoint.DistanceTo(cableTrayConnect2.m_CableTrayCenterLine.startPoint()); double distOther = iterF->second.endPoint.DistanceTo(cableTrayConnect2.m_CableTrayCenterLine.endPoint()); double distElbows1 = iterF->second.endPoint.DistanceTo(iterE->second.startPoint); double distElbows2 = iterF->second.endPoint.DistanceTo(iterE->second.endPoint); if (dist < 400 && (distElbows1 <= cableTrayConnect2.m_CableTrayRadius * 2 || distElbows2 <= cableTrayConnect2.m_CableTrayRadius * 2)) { iterF->second.ischecked = true; vClosetCableTrayElow2.push_back(make_pair(dist, iterF->second)); } else if (distOther < 400 && (distElbows1 <= cableTrayConnect1.m_CableTrayRadius * 2 || distElbows2 <= cableTrayConnect1.m_CableTrayRadius * 2)) { iterF->second.ischecked = true; vClosetCableTrayElow1.push_back(make_pair(distOther, iterF->second)); } } } } if (vClosetCableTrayElow1.size() > 0) { sort(vClosetCableTrayElow1.begin(), vClosetCableTrayElow1.end(), FindClostNum); cableTrayCount.push_back(vClosetCableTrayElow1[0].second); } if (vClosetCableTrayElow2.size() > 0) { sort(vClosetCableTrayElow2.begin(), vClosetCableTrayElow2.end(), FindClostNum); cableTrayCount.push_back(vClosetCableTrayElow2[0].second); } //MepBaseAPI mepBaseAPI; //一边都没有 弯头 if (cableTrayCount.size() == 1) { CString str = dmtl.NewGuid(); DwgElecCableTrayElbow elbow; elbow.m_ElbowObjID = str; elbow.m_ConnectLineObjID1 = cableTrayConnect1.m_CableTrayId; elbow.m_ConnectLineObjID2 = cableTrayConnect2.m_CableTrayId; mElecDataSet[iter->first].m_ElecCableTrayElows.push_back(elbow); } //有一边有弯头 三通 else if (cableTrayCount.size() == 2) { CString str = dmtl.NewGuid(); DwgElecCableTray3T CableTray3T; CableTray3T.m_CableTray3TID = str; CableTray3T.m_CableTray1 = cableTrayConnect1.m_CableTrayId; CableTray3T.m_CableTray2 = cableTrayConnect2.m_CableTrayId; for (auto elbow : cableTrayCount) { CableTray3T.m_elbowsConnected.push_back(elbow); } mElecDataSet[iter->first].m_ElecCableTray3Ts.push_back(CableTray3T); } //两边都有弯头 四通 else if (cableTrayCount.size() == 3) { CString str = dmtl.NewGuid(); DwgElecCableTray4T CableTray4T; CableTray4T.m_CableTray4TID = str; CableTray4T.m_CableTray1 = cableTrayConnect1.m_CableTrayId; CableTray4T.m_CableTray2 = cableTrayConnect2.m_CableTrayId; for (auto elbow : cableTrayCount) { CableTray4T.m_elbowsConnected.push_back(elbow); } mElecDataSet[iter->first].m_ElecCableTray4Ts.push_back(CableTray4T); } } //寻找三通四通的其他几个相连接的桥架 vector<pair<double, DwgElecCableTray>> vClosetCableTrayS4T1, vClosetCableTrayE3T, vClosetCableTrayE4T1, vClosetCableTrayS4T2, vClosetCableTrayE4T2; for (auto iter3T = mElecDataSet[iter->first].m_ElecCableTray3Ts.begin(); iter3T != mElecDataSet[iter->first].m_ElecCableTray3Ts.end(); iter3T++) { vector<pair<double, DwgElecCableTray>> vClosetCableTrayS3T; for (auto line : m_CableTray) { if (line.m_CableTrayId != iter3T->m_CableTray1 && line.m_CableTrayId != iter3T->m_CableTray2&&iter3T->m_elbowsConnected.size() >= 2) { if (line.m_CableTrayCenterLine.IsParallelWith(iter3T->m_elbowsConnected[1].startLine)) { double distS = line.m_CableTrayCenterLine.startPoint().DistanceTo(iter3T->m_elbowsConnected[1].startPoint); double distE = line.m_CableTrayCenterLine.endPoint().DistanceTo(iter3T->m_elbowsConnected[1].startPoint); if (distS > distE && distE < line.m_CableTrayRadius * 2) { vClosetCableTrayS3T.push_back(make_pair(distE, line)); } else if (distS < distE && distS < line.m_CableTrayRadius * 2) { vClosetCableTrayS3T.push_back(make_pair(distS, line)); } } if (line.m_CableTrayCenterLine.IsParallelWith(iter3T->m_elbowsConnected[1].endLine)) { double distS = line.m_CableTrayCenterLine.startPoint().DistanceTo(iter3T->m_elbowsConnected[1].endPoint); double distE = line.m_CableTrayCenterLine.endPoint().DistanceTo(iter3T->m_elbowsConnected[1].endPoint); if (distS > distE && distE < line.m_CableTrayRadius * 2) { vClosetCableTrayS3T.push_back(make_pair(distE, line)); } else if (distS < distE && distS < line.m_CableTrayRadius * 2) { vClosetCableTrayS3T.push_back(make_pair(distS, line)); } } if (line.m_CableTrayCenterLine.IsParallelWith(iter3T->m_elbowsConnected[0].startLine)) { double distS = line.m_CableTrayCenterLine.startPoint().DistanceTo(iter3T->m_elbowsConnected[0].startPoint); double distE = line.m_CableTrayCenterLine.endPoint().DistanceTo(iter3T->m_elbowsConnected[0].startPoint); if (distS > distE && distE < line.m_CableTrayRadius * 2) { vClosetCableTrayS3T.push_back(make_pair(distE, line)); } else if (distS < distE && distS < line.m_CableTrayRadius * 2) { vClosetCableTrayS3T.push_back(make_pair(distS, line)); } } if (line.m_CableTrayCenterLine.IsParallelWith(iter3T->m_elbowsConnected[0].endLine)) { double distS = line.m_CableTrayCenterLine.startPoint().DistanceTo(iter3T->m_elbowsConnected[0].endPoint); double distE = line.m_CableTrayCenterLine.endPoint().DistanceTo(iter3T->m_elbowsConnected[0].endPoint); if (distS > distE && distE < line.m_CableTrayRadius * 2) { vClosetCableTrayS3T.push_back(make_pair(distE, line)); } else if (distS < distE && distS < line.m_CableTrayRadius * 2) { vClosetCableTrayS3T.push_back(make_pair(distS, line)); } } } } if (vClosetCableTrayS3T.size() > 0) { sort(vClosetCableTrayS3T.begin(), vClosetCableTrayS3T.end(), FindClosetNum); iter3T->m_CableTray3 = vClosetCableTrayS3T[0].second.m_CableTrayId; } } //if (vClosetCableTrayE3T.size() > 0) //{ // sort(vClosetCableTrayE3T.begin(), vClosetCableTrayE3T.end(), FindClosetNum); // mElecDataSet[iter->first].m_ElecCableTray3Ts[mElecDataSet[iter->first].m_ElecCableTray3Ts.size() - 1].m_CableTray3 = vClosetCableTrayE3T[0].second.m_CableTrayId; //} for (auto iter4T = mElecDataSet[iter->first].m_ElecCableTray4Ts.begin(); iter4T != mElecDataSet[iter->first].m_ElecCableTray4Ts.end(); iter4T++) { for (auto line : m_CableTray) { if (line.m_CableTrayId != iter4T->m_CableTray1 && line.m_CableTrayId != iter4T->m_CableTray2) { if (line.m_CableTrayCenterLine.IsParallelWith(iter4T->m_elbowsConnected[0].startLine)) { double distS = line.m_CableTrayCenterLine.startPoint().DistanceTo(iter4T->m_elbowsConnected[0].startPoint); double distE = line.m_CableTrayCenterLine.endPoint().DistanceTo(iter4T->m_elbowsConnected[0].startPoint); if (distS > distE && distE < line.m_CableTrayRadius * 2) { vClosetCableTrayS4T1.push_back(make_pair(distE, line)); } else if (distS < distE && distS < line.m_CableTrayRadius * 2) { vClosetCableTrayS4T1.push_back(make_pair(distS, line)); } } if (line.m_CableTrayCenterLine.IsParallelWith(iter4T->m_elbowsConnected[0].endLine)) { double distS = line.m_CableTrayCenterLine.startPoint().DistanceTo(iter4T->m_elbowsConnected[0].endPoint); double distE = line.m_CableTrayCenterLine.endPoint().DistanceTo(iter4T->m_elbowsConnected[0].endPoint); if (distS > distE && distE < line.m_CableTrayRadius * 2) { vClosetCableTrayS4T1.push_back(make_pair(distE, line)); } else if (distS < distE && distS < line.m_CableTrayRadius * 2) { vClosetCableTrayS4T1.push_back(make_pair(distS, line)); } } if (line.m_CableTrayCenterLine.IsParallelWith(iter4T->m_elbowsConnected[1].startLine)) { double distS = line.m_CableTrayCenterLine.startPoint().DistanceTo(iter4T->m_elbowsConnected[1].startPoint); double distE = line.m_CableTrayCenterLine.endPoint().DistanceTo(iter4T->m_elbowsConnected[1].startPoint); if (distS > distE && distE < line.m_CableTrayRadius * 2) { vClosetCableTrayS4T1.push_back(make_pair(distE, line)); } else if (distS < distE && distS < line.m_CableTrayRadius * 2) { vClosetCableTrayS4T1.push_back(make_pair(distS, line)); } } if (line.m_CableTrayCenterLine.IsParallelWith(iter4T->m_elbowsConnected[1].endLine)) { double distS = line.m_CableTrayCenterLine.startPoint().DistanceTo(iter4T->m_elbowsConnected[1].endPoint); double distE = line.m_CableTrayCenterLine.endPoint().DistanceTo(iter4T->m_elbowsConnected[1].endPoint); if (distS > distE && distE < line.m_CableTrayRadius * 2) { vClosetCableTrayS4T1.push_back(make_pair(distE, line)); } else if (distS < distE && distS < line.m_CableTrayRadius * 2) { vClosetCableTrayS4T1.push_back(make_pair(distS, line)); } } if (line.m_CableTrayCenterLine.IsParallelWith(iter4T->m_elbowsConnected[2].startLine)) { double distS = line.m_CableTrayCenterLine.startPoint().DistanceTo(iter4T->m_elbowsConnected[2].startPoint); double distE = line.m_CableTrayCenterLine.endPoint().DistanceTo(iter4T->m_elbowsConnected[2].startPoint); if (distS > distE && distE < line.m_CableTrayRadius * 2) { vClosetCableTrayS4T2.push_back(make_pair(distE, line)); } else if (distS < distE && distS < line.m_CableTrayRadius * 2) { vClosetCableTrayS4T2.push_back(make_pair(distS, line)); } } if (line.m_CableTrayCenterLine.IsParallelWith(iter4T->m_elbowsConnected[2].endLine)) { double distS = line.m_CableTrayCenterLine.startPoint().DistanceTo(iter4T->m_elbowsConnected[2].endPoint); double distE = line.m_CableTrayCenterLine.endPoint().DistanceTo(iter4T->m_elbowsConnected[2].endPoint); if (distS > distE && distE < line.m_CableTrayRadius * 2) { vClosetCableTrayS4T2.push_back(make_pair(distE, line)); } else if (distS < distE && distS < line.m_CableTrayRadius * 2) { vClosetCableTrayS4T2.push_back(make_pair(distS, line)); } } } } if (vClosetCableTrayS4T1.size() > 0) { sort(vClosetCableTrayS4T1.begin(), vClosetCableTrayS4T1.end(), FindClosetNum); iter4T->m_CableTray3 = vClosetCableTrayS4T1[0].second.m_CableTrayId; } if (vClosetCableTrayS4T2.size() > 0) { sort(vClosetCableTrayS4T2.begin(), vClosetCableTrayS4T2.end(), FindClosetNum); iter4T->m_CableTray4 = vClosetCableTrayS4T2[0].second.m_CableTrayId; } } //寻找不是弯头但互相交叉的三通桥架 //vector<DwgElecCableTray> vNewCableTray; for (auto crossLine : m_CableTray) { for (auto crossedLine : m_CableTray) { geoPoint3d perpPt; double distS, distE; DwgElecCableTray3T unNormal3T; //找不相交但是构成三通的位置 if (crossedLine.m_CableTrayCenterLine.GetPerpPt(crossLine.m_CableTrayCenterLine.startPoint(), perpPt) == 0) { distS = perpPt.DistanceTo(crossLine.m_CableTrayCenterLine.startPoint()); } else if (crossedLine.m_CableTrayCenterLine.GetPerpPt(crossLine.m_CableTrayCenterLine.endPoint(), perpPt) == 0) { distE = perpPt.DistanceTo(crossLine.m_CableTrayCenterLine.endPoint()); } if (distS > distE && distE <= crossLine.m_CableTrayRadius) { unNormal3T.m_CableTray1 = crossLine.m_CableTrayId; unNormal3T.m_CableTray2 = crossedLine.m_CableTrayId; } else if (distS < distE && distS <= crossLine.m_CableTrayRadius) { unNormal3T.m_CableTray1 = crossLine.m_CableTrayId; unNormal3T.m_CableTray2 = crossedLine.m_CableTrayId; } } } //for (auto lines : m_CableTray) //{ // //} // for (auto connectTray : m_CableTray) // { // if (connectTray.m_CableTrayLine1.startPoint().DistanceTo(iter->second.startPoint) < 10 && // connectTray.m_CableTrayLine1.IsParallelWith(iter->second.startLine)) // { // connectTray.m_CableTrayLine1HasElbow = true; // vElbowCount1.push_back(iter->second); // 记录下这个桥架是否还与另外的弯头相连 // for (auto iterr = vElbows.begin(); iterr != vElbows.end(); iterr++) // { // if (iter->first != iterr->first) // { // if (iterr->second.startPoint.DistanceTo(connectTray.m_CableTrayLine2.startPoint) < 10 && // iterr->second.startLine.IsParallelWith(connectTray.m_CableTrayLine2)) // { // } // else if (iterr->second.endPoint.DistanceTo(connectTray.m_CableTrayLine2.startPoint) < 10 && // iterr->second.endLine.IsParallelWith(connectTray.m_CableTrayLine2)) // { // } // } // } // } // else if (connectTray.m_CableTrayLine1.endPoint().DistanceTo(iter->second.startPoint) < 10 && // connectTray.m_CableTrayLine1.IsParallelWith(iter->second.startLine)) // { // connectTray.m_CableTrayLine1HasElbow = true; // vElbowCounts.push_back(iter->second); // for (auto iterr = vElbows.begin(); iterr != vElbows.end(); iterr++) // { // if (iter->first != iterr->first) // { // } // } // } // else if (connectTray.m_CableTrayLine2.startPoint().DistanceTo(iter->second.startPoint) < 10 && // connectTray.m_CableTrayLine2.IsParallelWith(iter->second.startLine)) // { // connectTray.m_CableTrayLine2HasElbow = true; // vElbowCounts.push_back(iter->second); // } // else if (connectTray.m_CableTrayLine2.endPoint().DistanceTo(iter->second.startPoint) < 10 && // connectTray.m_CableTrayLine2.IsParallelWith(iter->second.startLine)) // { // connectTray.m_CableTrayLine1HasElbow = true; // vElbowCounts.push_back(iter->second); // for (auto iterr = vElbows.begin(); iterr != vElbows.end(); iterr++) // { // if (iter->first != iterr->first) // { // } // } // } // else if (connectTray.m_CableTrayLine1.startPoint().DistanceTo(iter->second.endPoint) < 10 && // connectTray.m_CableTrayLine1.IsParallelWith(iter->second.endLine)) // { // connectTray.m_CableTrayLine1HasElbow = true; // vElbowCounts.push_back(iter->second); // } // else if (connectTray.m_CableTrayLine1.endPoint().DistanceTo(iter->second.endPoint) < 10 && // connectTray.m_CableTrayLine1.IsParallelWith(iter->second.endLine)) // { // connectTray.m_CableTrayLine1HasElbow = true; // vElbowCounts.push_back(iter->second); // } // else if (connectTray.m_CableTrayLine2.startPoint().DistanceTo(iter->second.endPoint) < 10 && // connectTray.m_CableTrayLine2.IsParallelWith(iter->second.endLine)) // { // connectTray.m_CableTrayLine1HasElbow = true; // vElbowCounts.push_back(iter->second); // } // else if (connectTray.m_CableTrayLine2.endPoint().DistanceTo(iter->second.endPoint) < 10 && // connectTray.m_CableTrayLine2.IsParallelWith(iter->second.endLine)) // { // connectTray.m_CableTrayLine2HasElbow = true; // vElbowCounts.push_back(iter->second); // } // } // if (vElbowCounts.size() == 2) // { // iter->second.connectLine1 = vElbowCounts[0].startLine; // } //} //mElecDataSet[iter->first].m_ElecCableTrays = m_CableTray; //找到每一个桥架的中心线并截断不规则的桥架线段 /* for (auto tray : m_CableTray) { geoSegLine centerLine; geoPoint3d perPt1, perPt2, perPt3, perPt4; vector<pair<geoPoint3d,geoPoint3d>> perPoints; vector<geoPoint3d> pointsOnLine; //找到两条线的每一条垂足 if (tray.m_CableTrayLine2.GetPerpPt(tray.m_CableTrayLine1.startPoint(), perPt1) == 0) { perPoints.push_back(make_pair(perPt1, tray.m_CableTrayLine1.startPoint())); } else { pointsOnLine.push_back(perPt1); } if (tray.m_CableTrayLine2.GetPerpPt(tray.m_CableTrayLine1.endPoint(), perPt2) == 0) { perPoints.push_back(make_pair(perPt2, tray.m_CableTrayLine1.endPoint())); } else { pointsOnLine.push_back(perPt2); } if (tray.m_CableTrayLine1.GetPerpPt(tray.m_CableTrayLine2.startPoint(), perPt3) == 0) { perPoints.push_back(make_pair(perPt3, tray.m_CableTrayLine2.startPoint())); } else { pointsOnLine.push_back(perPt3); } if (tray.m_CableTrayLine1.GetPerpPt(tray.m_CableTrayLine2.endPoint(), perPt4) == 0) { perPoints.push_back(make_pair(perPt4, tray.m_CableTrayLine2.endPoint())); } else { pointsOnLine.push_back(perPt4); } //两个点说明都是最小值 if (perPoints.size() == 2) { geoPoint3d startPt, endPt; startPt.x = (perPoints[0].first.x + perPoints[0].second.x) / 2; startPt.y = (perPoints[0].first.y + perPoints[0].second.y) / 2; endPt.x = (perPoints[1].first.x + perPoints[1].second.x) / 2; endPt.y = (perPoints[1].first.y + perPoints[1].second.y) / 2; centerLine.Set(startPt, endPt); } //四个点说明是矩形 if (pointsOnLine.size() == 4) { geoPoint3d startPt, endPt; if (tray.m_CableTrayLine1.startPoint().DistanceTo(tray.m_CableTrayLine2.startPoint()) > tray.m_CableTrayLine1.startPoint().DistanceTo(tray.m_CableTrayLine2.endPoint())) { startPt.x = (tray.m_CableTrayLine1.startPoint().x + tray.m_CableTrayLine2.endPoint().x) / 2; startPt.y = (tray.m_CableTrayLine1.startPoint().y + tray.m_CableTrayLine2.endPoint().y) / 2; endPt.x = (tray.m_CableTrayLine1.endPoint().x + tray.m_CableTrayLine2.startPoint().x) / 2; endPt.y = (tray.m_CableTrayLine1.endPoint().y + tray.m_CableTrayLine2.startPoint().y) / 2; } else if (tray.m_CableTrayLine1.startPoint().DistanceTo(tray.m_CableTrayLine2.startPoint()) < tray.m_CableTrayLine1.startPoint().DistanceTo(tray.m_CableTrayLine2.endPoint())) { startPt.x = (tray.m_CableTrayLine1.startPoint().x + tray.m_CableTrayLine2.startPoint().x) / 2; startPt.y = (tray.m_CableTrayLine1.startPoint().y + tray.m_CableTrayLine2.startPoint().y) / 2; endPt.x = (tray.m_CableTrayLine1.endPoint().x + tray.m_CableTrayLine2.endPoint().x) / 2; endPt.y = (tray.m_CableTrayLine1.endPoint().y + tray.m_CableTrayLine2.endPoint().y) / 2; } centerLine.Set(startPt, endPt); } //直角梯形 if (perPoints.size() == 1 && pointsOnLine.size() == 3) { geoPoint3d startPt, endPt; startPt.x = (perPoints[0].first.x + perPoints[0].second.x) / 2; startPt.y = (perPoints[0].first.y + perPoints[0].second.y) / 2; vector<geoPoint3d> realPoints; for (auto point : pointsOnLine) { if (point.DistanceTo(tray.m_CableTrayLine1.startPoint()) < 10 || point.DistanceTo(tray.m_CableTrayLine1.endPoint()) < 10 || point.DistanceTo(tray.m_CableTrayLine2.startPoint()) < 10 || point.DistanceTo(tray.m_CableTrayLine2.endPoint()) < 10) { realPoints.push_back(point); } } if (realPoints.size() >= 2) { endPt.x = (realPoints[0].x + realPoints[1].x) / 2; endPt.y = (realPoints[0].y + realPoints[1].y) / 2; } centerLine.Set(startPt, endPt); } tray.m_CableTrayCenterLine = centerLine; } */ //分析三通,四通和弯头 //for (auto mutliTray : m_CableTray) //{ // for (auto temp : m_CableTray) // { // if () // { // } // } //} }
最新发布
08-09
using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Geometry; using Autodesk.AutoCAD.GraphicsInterface; using Autodesk.AutoCAD.Runtime; using System; using System.Collections.Generic; using System.Linq; // 解决Exception命名冲突 using SysException = System.Exception; using AcException = Autodesk.AutoCAD.Runtime.Exception; namespace GridLinePlugin { public class GridLineCommands { // 固定线段长度选项(毫米) private static readonly double[] FixedLengths = { 300, 600, 900, 1200, 1500, 1800 }; private const string LAYER_NAME = "盘扣轴网"; // 参数记忆静态变量 private static double _lastOffsetX = 0.0; private static double _lastOffsetY = 0.0; private static double _lastStartLengthX = 900.0; private static double _lastStartLengthY = 1500.0; // 辅助方法移动到类顶部 private double GetOffset(string message, double defaultValue, Editor ed) { string formattedMessage = string.Format(message, defaultValue); PromptDoubleOptions opts = new PromptDoubleOptions(formattedMessage) { AllowNegative = true, AllowZero = true, DefaultValue = defaultValue, UseDefaultValue = true }; PromptDoubleResult res = ed.GetDouble(opts); return res.Status == PromptStatus.OK ? res.Value : defaultValue; } private double GetSegmentLength(string message, double defaultValue, Editor ed) { string formattedMessage = string.Format(message, defaultValue); PromptDoubleOptions opts = new PromptDoubleOptions(formattedMessage) { AllowNegative = false, AllowZero = false, AllowNone = false, DefaultValue = defaultValue, UseDefaultValue = true }; PromptDoubleResult res; do { res = ed.GetDouble(opts); if (res.Status != PromptStatus.OK) return defaultValue; if (res.Value <= 0) { ed.WriteMessage("\n长度必须大于0! 请重新输入: "); } } while (res.Value <= 0); return res.Value; } [CommandMethod("DG", CommandFlags.Modal)] public void DrawGridInteractive() { Document doc = Application.DocumentManager.MdiActiveDocument; Editor ed = doc.Editor; try { // 1. 获取偏移值 double offsetX = GetOffset("\nX方向偏移值 (mm) <{0}>: ", _lastOffsetX, ed); double offsetY = GetOffset("\nY方向偏移值 (mm) <{0}>: ", _lastOffsetY, ed); // 2. 获取起始长度 double startLengthX = GetSegmentLength("\n横向起始线段长度 (mm) <{0}>: ", _lastStartLengthX, ed); double startLengthY = GetSegmentLength("\n纵向起始线段长度 (mm) <{0}>: ", _lastStartLengthY, ed); // 更新记忆参数 _lastOffsetX = offsetX; _lastOffsetY = offsetY; _lastStartLengthX = startLengthX; _lastStartLengthY = startLengthY; // 3. 获取起始点 PromptPointResult startPtRes = ed.GetPoint("\n选择网格起始点: "); if (startPtRes.Status != PromptStatus.OK) return; Point3d basePoint = startPtRes.Value; // 应用偏移 Point3d actualStartPoint = basePoint + new Vector3d(offsetX, offsetY, 0); // 4. 创建并运行拖拽器 GridJig jig = new GridJig(actualStartPoint, startLengthX, startLengthY, doc); PromptResult jigResult = ed.Drag(jig); if (jigResult.Status == PromptStatus.OK) { jig.CommitGrid(LAYER_NAME); int shortLines = jig.GetShortLineCount(300.0); if (shortLines > 0) { ed.WriteMessage($"\n警告: 有 {shortLines} 条线段长度小于300mm!"); } ed.WriteMessage($"\n成功绘制网格! 共生成 {jig.LineCount} 条线段"); } } catch (SysException ex) // 使用别名解决冲突 { ed.WriteMessage($"\n错误: {ex.Message}"); } } [CommandMethod("CO", CommandFlags.Modal)] public void CheckOverlap() { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; try { // 让用户框选线段 PromptSelectionOptions selOpts = new PromptSelectionOptions { MessageForAdding = "请框选需要检查重叠的线段: " }; PromptSelectionResult selRes = ed.GetSelection(selOpts); if (selRes.Status != PromptStatus.OK) return; using (Transaction tr = db.TransactionManager.StartTransaction()) { BlockTableRecord btr = tr.GetObject( SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForWrite) as BlockTableRecord; // 收集选中的线段 List<Line> allLines = new List<Line>(); foreach (SelectedObject so in selRes.Value) { Line line = tr.GetObject(so.ObjectId, OpenMode.ForRead) as Line; if (line != null && line.Layer == LAYER_NAME) { allLines.Add(line); } } // 分组处理:水平线和垂直线 List<Line> horizontalLines = new List<Line>(); List<Line> verticalLines = new List<Line>(); const double angleTolerance = 0.01; // 角度容差(弧度) foreach (Line line in allLines) { Vector3d dir = (line.EndPoint - line.StartPoint).GetNormal(); double angle = dir.GetAngleTo(Vector3d.XAxis); // 水平线(0°或180°) if (angle < angleTolerance || Math.Abs(angle - Math.PI) < angleTolerance) { horizontalLines.Add(line); } // 垂直线(90°或270°) else if (Math.Abs(angle - Math.PI / 2) < angleTolerance || Math.Abs(angle - 3 * Math.PI / 2) < angleTolerance) { verticalLines.Add(line); } } // 处理水平线重叠 ProcessOverlappingLines(horizontalLines, true, tr, btr); // 处理垂直线重叠 ProcessOverlappingLines(verticalLines, false, tr, btr); tr.Commit(); ed.WriteMessage("\n重叠处理完成!"); } } catch (SysException ex) { ed.WriteMessage($"\n错误: {ex.Message}"); } } // 处理同一方向上的线段重叠 private void ProcessOverlappingLines(List<Line> lines, bool isHorizontal, Transaction tr, BlockTableRecord btr) { if (lines.Count < 2) return; // 按坐标排序(水平线按Y,垂直线按X) lines.Sort((a, b) => isHorizontal ? a.StartPoint.Y.CompareTo(b.StartPoint.Y) : a.StartPoint.X.CompareTo(b.StartPoint.X)); // 分组:相同坐标位置的线段为一组 var groups = lines.GroupBy(line => isHorizontal ? Math.Round(line.StartPoint.Y, 4) : Math.Round(line.StartPoint.X, 4) ); foreach (var group in groups) { // 每组内按起点位置排序 List<Line> sortedLines = isHorizontal ? group.OrderBy(l => Math.Min(l.StartPoint.X, l.EndPoint.X)).ToList() : group.OrderBy(l => Math.Min(l.StartPoint.Y, l.EndPoint.Y)).ToList(); bool modified; do { modified = false; for (int i = 0; i < sortedLines.Count - 1; i++) { Line line1 = sortedLines[i]; Line line2 = sortedLines[i + 1]; // 获取线段范围 double start1, end1, start2, end2; GetLineRange(line1, isHorizontal, out start1, out end1); GetLineRange(line2, isHorizontal, out start2, out end2); // 检查重叠 if (end1 > start2 && end2 > start1) { double overlapStart = Math.Max(start1, start2); double overlapEnd = Math.Min(end1, end2); // 分割线段并删除重叠部分 List<Line> newParts1 = SplitLine(line1, overlapStart, overlapEnd, isHorizontal, tr, btr); List<Line> newParts2 = SplitLine(line2, overlapStart, overlapEnd, isHorizontal, tr, btr); // 移除原线段 line1.UpgradeOpen(); line1.Erase(); line2.UpgradeOpen(); line2.Erase(); // 添加新分段 sortedLines.RemoveRange(i, 2); sortedLines.InsertRange(i, newParts1); sortedLines.InsertRange(i + newParts1.Count, newParts2); modified = true; break; // 重新开始检查 } } } while (modified); // 重复直到无重叠 } } // 获取线段在指定方向的范围 private void GetLineRange(Line line, bool isHorizontal, out double start, out double end) { if (isHorizontal) { start = Math.Min(line.StartPoint.X, line.EndPoint.X); end = Math.Max(line.StartPoint.X, line.EndPoint.X); } else { start = Math.Min(line.StartPoint.Y, line.EndPoint.Y); end = Math.Max(line.StartPoint.Y, line.EndPoint.Y); } } // 分割线段并删除重叠部分 private List<Line> SplitLine(Line line, double overlapStart, double overlapEnd, bool isHorizontal, Transaction tr, BlockTableRecord btr) { List<Line> newLines = new List<Line>(); double lineStart, lineEnd; GetLineRange(line, isHorizontal, out lineStart, out lineEnd); // 前段(重叠前) if (lineStart < overlapStart - 0.001) // 避免浮点误差 { Line frontPart = CreateLineSegment(line, lineStart, overlapStart, isHorizontal); btr.AppendEntity(frontPart); tr.AddNewlyCreatedDBObject(frontPart, true); newLines.Add(frontPart); } // 后段(重叠后) if (lineEnd > overlapEnd + 0.001) { Line backPart = CreateLineSegment(line, overlapEnd, lineEnd, isHorizontal); btr.AppendEntity(backPart); tr.AddNewlyCreatedDBObject(backPart, true); newLines.Add(backPart); } return newLines; } // 创建线段分段 private Line CreateLineSegment(Line original, double startVal, double endVal, bool isHorizontal) { Point3d startPt, endPt; if (isHorizontal) { startPt = new Point3d(startVal, original.StartPoint.Y, 0); endPt = new Point3d(endVal, original.StartPoint.Y, 0); } else { startPt = new Point3d(original.StartPoint.X, startVal, 0); endPt = new Point3d(original.StartPoint.X, endVal, 0); } Line newLine = new Line(startPt, endPt) { Layer = original.Layer, ColorIndex = original.ColorIndex, Linetype = original.Linetype, LinetypeScale = original.LinetypeScale }; return newLine; } // 检查两条线是否重叠 private bool AreLinesOverlapping(Line line1, Line line2) { const double tolerance = 0.001; Vector3d dir1 = line1.EndPoint - line1.StartPoint; Vector3d dir2 = line2.EndPoint - line2.StartPoint; if (!dir1.IsParallelTo(dir2, new Tolerance(tolerance, tolerance))) return false; Vector3d toLine2Start = line2.StartPoint - line1.StartPoint; if (!toLine2Start.IsPerpendicularTo(dir1, new Tolerance(tolerance, tolerance))) return false; double min1 = Math.Min(0, 1); double max1 = Math.Max(0, 1); double startParam = GetLineParameter(line1, line2.StartPoint); double endParam = GetLineParameter(line1, line2.EndPoint); double min2 = Math.Min(startParam, endParam); double max2 = Math.Max(startParam, endParam); return (min1 <= max2 + tolerance && max1 >= min2 - tolerance); } // 计算点在直线上的参数位置 private double GetLineParameter(Line line, Point3d point) { Vector3d lineVec = line.EndPoint - line.StartPoint; Vector3d pointVec = point - line.StartPoint; double dotProduct = lineVec.DotProduct(pointVec); double lineLengthSq = lineVec.DotProduct(lineVec); return lineLengthSq > 0 ? dotProduct / lineLengthSq : 0; } // 网格拖拽器类 private class GridJig : DrawJig { private readonly Point3d _startPoint; private Point3d _currentPoint; private readonly double _startLengthX; private readonly double _startLengthY; private readonly Document _doc; private readonly List<Line> _lines = new List<Line>(); private List<double> _xSegments = new List<double>(); private List<double> _ySegments = new List<double>(); public int LineCount => _lines.Count; public int GetShortLineCount(double minLength) { return _lines.Count(line => line.Length < minLength); } public GridJig(Point3d startPoint, double startLengthX, double startLengthY, Document doc) { _startPoint = startPoint; _currentPoint = startPoint; _startLengthX = startLengthX; _startLengthY = startLengthY; _doc = doc; } // 实现抽象成员 protected override SamplerStatus Sampler(JigPrompts prompts) { JigPromptPointOptions opts = new JigPromptPointOptions("\n指定网格终点或输入长度: ") { UserInputControls = UserInputControls.Accept3dCoordinates | UserInputControls.NoZeroResponseAccepted | UserInputControls.NoNegativeResponseAccepted, BasePoint = _startPoint, UseBasePoint = true, Cursor = CursorType.RubberBand }; PromptPointResult result = prompts.AcquirePoint(opts); if (result.Status == PromptStatus.Cancel) return SamplerStatus.Cancel; if (_currentPoint.DistanceTo(result.Value) < 0.001) return SamplerStatus.NoChange; _currentPoint = result.Value; return SamplerStatus.OK; } protected override bool WorldDraw(WorldDraw draw) { try { // 清除临时图形 foreach (var line in _lines) { if (!line.IsDisposed) line.Dispose(); } _lines.Clear(); // 计算方向向量 Vector3d delta = _currentPoint - _startPoint; double dx = delta.X; double dy = delta.Y; // 计算总长度和方向 double totalX = Math.Abs(dx); double totalY = Math.Abs(dy); int signX = Math.Sign(dx); int signY = Math.Sign(dy); // 生成分段点 _xSegments = GenerateSegments(totalX, _startLengthX, true); _ySegments = GenerateSegments(totalY, _startLengthY, false); // 调试输出 _doc.Editor.WriteMessage($"\nX方向分段数: {_xSegments.Count}, Y方向分段数: {_ySegments.Count}"); // 计算实际终点 Point3d actualEndPoint = new Point3d( _startPoint.X + signX * _xSegments.Last(), _startPoint.Y + signY * _ySegments.Last(), _startPoint.Z ); // 绘制横向网格线 DrawHorizontalLines(signX, signY, actualEndPoint, draw); // 绘制纵向网格线 DrawVerticalLines(signX, signY, actualEndPoint, draw); } catch (SysException ex) { _doc.Editor.WriteMessage($"\n绘图错误: {ex.Message}"); } return true; } // 分段生成算法 private List<double> GenerateSegments(double totalLength, double spacing, bool isXDirection) { List<double> segments = new List<double> { 0 }; if (totalLength <= 0) return segments; double currentPos = 0; int segmentCount = 0; while (currentPos < totalLength && segmentCount < 100) { double remaining = totalLength - currentPos; // 尝试使用固定值 double segmentLength = FixedLengths .Where(len => len <= remaining) .DefaultIfEmpty(0) .Max(); // 如果没有合适固定值,尝试使用起始间距 if (segmentLength < 300 && spacing <= remaining) { segmentLength = spacing; } // 如果仍然不满足条件,跳过此段 if (segmentLength < 300) { break; } currentPos += segmentLength; // 确保不超过总长度 if (currentPos > totalLength) { currentPos = totalLength; } segments.Add(currentPos); segmentCount++; if (currentPos >= totalLength) break; } // 确保包含终点(如果剩余长度不足300mm则跳过) if (segments.Last() < totalLength && (totalLength - segments.Last()) >= 300) { segments.Add(totalLength); } // 确保至少有两个分段点 if (segments.Count < 2) { segments.Add(totalLength); } return segments; } // 绘制横向线 private void DrawHorizontalLines(int signX, int signY, Point3d actualEndPoint, WorldDraw draw) { if (_ySegments.Count == 0) return; for (int i = 0; i < _ySegments.Count; i++) { double yPos = _ySegments[i]; Point3d start = _startPoint + new Vector3d(0, signY * yPos, 0); // 终点自动修剪到实际终点 Point3d end = new Point3d(actualEndPoint.X, start.Y, start.Z); Line line = new Line(start, end); line.ColorIndex = 1; if (line.Length < 0.001) continue; if (!IsOverlapping(line)) { _lines.Add(line); draw.Geometry.Draw(line); } } } // 绘制纵向线 private void DrawVerticalLines(int signX, int signY, Point3d actualEndPoint, WorldDraw draw) { if (_xSegments.Count == 0) return; for (int i = 0; i < _xSegments.Count; i++) { double xPos = _xSegments[i]; Point3d start = _startPoint + new Vector3d(signX * xPos, 0, 0); // 终点自动修剪到实际终点 Point3d end = new Point3d(start.X, actualEndPoint.Y, start.Z); Line line = new Line(start, end); line.ColorIndex = 1; if (line.Length < 0.001) continue; if (!IsOverlapping(line)) { _lines.Add(line); draw.Geometry.Draw(line); } } } // 重叠检测方法 private bool IsOverlapping(Line newLine) { const double toleranceValue = 0.1; Tolerance tolerance = new Tolerance(toleranceValue, toleranceValue); foreach (Line existingLine in _lines) { bool sameStart = newLine.StartPoint.IsEqualTo(existingLine.StartPoint, tolerance) || newLine.StartPoint.IsEqualTo(existingLine.EndPoint, tolerance); bool sameEnd = newLine.EndPoint.IsEqualTo(existingLine.StartPoint, tolerance) || newLine.EndPoint.IsEqualTo(existingLine.EndPoint, tolerance); if (sameStart && sameEnd) { return true; } } return false; } // 提交网格到图纸 public void CommitGrid(string layerName) { using (Transaction tr = _doc.Database.TransactionManager.StartTransaction()) { try { BlockTable bt = tr.GetObject(_doc.Database.BlockTableId, OpenMode.ForRead) as BlockTable; BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; // 创建图层 LayerTable lt = tr.GetObject(_doc.Database.LayerTableId, OpenMode.ForRead) as LayerTable; if (!lt.Has(layerName)) { lt.UpgradeOpen(); LayerTableRecord layer = new LayerTableRecord { Name = layerName, Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex( Autodesk.AutoCAD.Colors.ColorMethod.ByAci, 1) }; lt.Add(layer); tr.AddNewlyCreatedDBObject(layer, true); } // 添加所有线段(最终修剪) foreach (Line line in _lines) { if (!line.IsDisposed) { // 精确修剪到网格边界 TrimToGridBoundary(line); // 跳过小于300mm的线段 if (line.Length < 300) continue; line.Layer = layerName; line.ColorIndex = 256; // ByLayer btr.AppendEntity(line); tr.AddNewlyCreatedDBObject(line, true); } } tr.Commit(); } catch (SysException) { tr.Abort(); throw; } } } // 精确修剪线段到网格边界 private void TrimToGridBoundary(Line line) { // 计算实际网格边界 double maxX = _xSegments.Last(); double maxY = _ySegments.Last(); // 获取线段的实际方向 Vector3d direction = line.EndPoint - line.StartPoint; // 横向线修剪到最大Y位置 if (Math.Abs(direction.X) > Math.Abs(direction.Y)) { double currentY = Math.Abs(line.EndPoint.Y - _startPoint.Y); if (currentY > maxY) { line.EndPoint = new Point3d( line.EndPoint.X, _startPoint.Y + Math.Sign(direction.Y) * maxY, line.EndPoint.Z); } } // 纵向线修剪到最大X位置 else { double currentX = Math.Abs(line.EndPoint.X - _startPoint.X); if (currentX > maxX) { line.EndPoint = new Point3d( _startPoint.X + Math.Sign(direction.X) * maxX, line.EndPoint.Y, line.EndPoint.Z); } } } } } } CAD运行中存在两个问题 1、DG功能布线的时候未按指定横向和纵向输入的值进行连续布置 2、CO功能修剪重叠线后,删掉重叠线段至少要保留一根,
07-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值