#include "StdAfx.h" #include ".\itemdelete.h" #include "SigmaDoc.h" #include "SigmaView.h" #include "ActionDeleteItem.h" CItemDelete::CItemDelete(CSigmaDoc * ppDoc) : CItemFocusRect(ppDoc) , strLayer(_T("Layer:\\Condition")) , m_bIsRegionSelected(false) { SetUpdatePropertyGridState(FALSE); m_idea=DELETE_DEFAULT; this->SetType(ITEM_DELETE); condition = 0; m_pSelectCurve=new CItemSelectCurve(GetDoc()); m_pSelectCurve->SetNumber(-1); m_pSelectCurve->EnableSelectOtherType(TRUE); m_pSelectCurve->AddOtherType(DOUBLEFOX_ELLIPSE); m_pSelectCurve->AddOtherType(DOUBLEFOX_ARC); m_pSelectCurve->AddOtherType(DOUBLEFOX_CIRCLE); m_pSelectCurve->AddOtherType(DOUBLEFOX_CRECT); value=0.2001; sel=0; mode=0; } CItemDelete::~CItemDelete(void) { if(m_pSelectCurve) delete m_pSelectCurve; m_pSelectCurve=NULL; } void CItemDelete::DoLButtonUp(CDC *pDC) { switch(m_idea) { default: case DELETE_DEFAULT: Delete(); break; case DELETE_IN: if (m_pSelectCurve->GetSelectCount() > 0) { //c = DeleteIn(); DeleteIn(); } break; case DELETE_OUT: if (m_pSelectCurve->GetSelectCount() > 0) { //c = DeleteOut(); DeleteOut(); } break; case DELETE_CONDITION: //c=DeleteCondition(); DeleteCondition(NULL); break; } } void CItemDelete::OnLButtonDown(CDC *pDC, UINT nFlags, CPoint point, int vk) { GetDC()->Create(pDC); //修复CItemSelectCurve::DrawSelectItem时崩溃的问题。 CPoint2D dp = GetDC()->GetReal(point); c_down = dp; c_last = dp; switch(m_idea) { default: case DELETE_DEFAULT: case DELETE_CONDITION: CItemFocusRect::OnLButtonDown(pDC, nFlags,point, vk); break; case DELETE_IN: case DELETE_OUT: m_pSelectCurve->OnLButtonDown(pDC, nFlags,point, vk); if(m_pSelectCurve->GetSelectCount()>0) DoLButtonUp(pDC); break; } } int CItemDelete::OnMouseMove(CDC *pDC, UINT nFlags, CPoint point) { switch(m_idea) { default: case DELETE_DEFAULT: case DELETE_CONDITION: CItemFocusRect::OnMouseMove(pDC, nFlags,point); break; case DELETE_IN: case DELETE_OUT: m_pSelectCurve->OnMouseMove(pDC, nFlags, point); break; } return 1; } int CItemDelete::SetProcessIdea(int nIdea) { if (nIdea < 0 || nIdea > 3) return -1; m_idea = nIdea; return 1; } bool CItemDelete::IsRegionSelected() { return m_bIsRegionSelected; } //修改:原来是在这个函数,选中了图元就删除。现在改成选中了图元保存起来 //这样做的目的是支持这样操作:没选中图元,不刷新了图形。这个操作,由上层调用者实现。 int CItemDelete::Delete(void) { m_bIsRegionSelected = false; CRect8 rect=GetRect(); m_selectionSet.RemoveAll(); GetDoc()->GetDraw()->IsInRange(rect, m_selectionSet); if(m_selectionSet.GetSize() == 0) return 0; //支持Undo Redo CActionDeleteItem * pAction = new CActionDeleteItem(GetDoc(), IDS_STRING_ACTION_DELETE, m_selectionSet); GetDoc()->SetActionItem(pAction); return 1; } int CItemDelete::GetSubMenu() { //if(m_idea==DELETE_IN) // return 14; return CItem::GetSubMenu(); } void CItemDelete::DeleteIn(void) { ASSERT(m_pSelectCurve); m_bIsRegionSelected = false; POSITION pos=m_pSelectCurve->GetSelectItem(0); if(pos==NULL) return ; //CList list; m_selectionSet.RemoveAll(); GetSelectWidthRegion(m_selectionSet,pos,0); GetDoc()->SetActionItem(new CActionDeleteItem(GetDoc(), IDS_STRING_ACTION_DELETE, m_selectionSet)); m_bIsRegionSelected = true; m_pSelectCurve->RemoveAllSelect(); } void CItemDelete::DeleteOut(void) { ASSERT(m_pSelectCurve); m_bIsRegionSelected = false; POSITION pos=m_pSelectCurve->GetSelectItem(0); if(pos==NULL) return ; m_selectionSet.RemoveAll(); GetSelectWidthRegion(m_selectionSet,pos,1); GetDoc()->SetActionItem(new CActionDeleteItem(GetDoc(), IDS_STRING_ACTION_DELETE, m_selectionSet)); m_bIsRegionSelected = true; m_pSelectCurve->RemoveAllSelect(); } //nOutSelect=0为在区域内,=1为不在区域内,=2为部分在部分不在,=3为全部在或者部分在的元素 int CItemDelete::GetSelectWidthRegion(CPositionList& SelectList, POSITION curvePostion, int nOutSelect) { COne* pOne; pOne=GetDoc()->GetDraw()->GetAt(curvePostion); CRect8 range=pOne->GetRect(); BOOL bDeleteCurve=FALSE; CCurveEx* pRgn=m_pSelectCurve->GetRangeCurve(curvePostion, bDeleteCurve); if(pRgn==NULL) return 0; int bInRgn; POSITION p,pos; CPtrList* pl=GetDoc()->GetDraw()->GetValueList(); pos = pl->GetHeadPosition(); while(pos) { p=pos; pOne=(COne*)pl->GetNext(pos); if(p==curvePostion)continue; if(!pOne->IsCanEdit() || !pOne->IsView()) continue; bInRgn=IsInside(pRgn, range, pOne); switch(nOutSelect) { case 0://区域内 if(bInRgn==1) SelectList.AddTail(p); break; case 1://区域外 if(bInRgn==0) SelectList.AddTail(p); break; case 2: if(bInRgn==2) SelectList.AddTail(p); break; case 3: if(bInRgn>0) SelectList.AddTail(p); break; } } if(bDeleteCurve) delete pRgn; return (int)SelectList.GetCount(); } int NItem::CItemDelete::IsInside(CCurveEx* pRgn, CRect8& rgnRange, COne* pOne) { int bInRgn=0; switch(pOne->GetType()) { case DOUBLEFOX_TEXT: { CText* pText=(CText*)pOne->GetValue(); if(rgnRange.PtInRect(pText->x0,pText->y0)) bInRgn=pRgn->IsInside(pText->x0,pText->y0); } case DOUBLEFOX_XYZ: case DOUBLEFOX_POINT: { CPointNameBase* pPoint=(CPointNameBase*)pOne->GetValue(); if(rgnRange.PtInRect(pPoint->x0,pPoint->y0)) bInRgn=pRgn->IsInside(pPoint->x0,pPoint->y0); } break; case DOUBLEFOX_CURVE: { CCurveEx* pCurve=(CCurveEx*)pOne->GetValue(); //0为全不在范围内,1为全在范围内,2为部分在部分不在范围内 bInRgn=CurveInRegion(pCurve,pRgn); } break; case DOUBLEFOX_ARC: { CArc* pc=(CArc*)pOne->GetValue(); CCurveEx* pCurve=pc->GetCurve(); bInRgn=CurveInRegion(pCurve,pRgn); } break; case DOUBLEFOX_CIRCLE: case DOUBLEFOX_ELLIPSE: { CEllipse* pc=(CEllipse*)pOne->GetValue(); CCurveEx* pCurve=pc->ToCurve(1.0); bInRgn=CurveInRegion(pCurve,pRgn); delete pCurve; } break; default: { CRect8 rt=pOne->GetRect(); CCurveEx* pCurve=new CCurveEx; pCurve->CreateCurveFromRect(&rt); bInRgn=CurveInRegion(pCurve,pRgn); delete pCurve; } break; } return bInRgn; } BOOL CItemDelete::ElementInRegion(COne* pOne, CPositionList& rgnList) //元素是否在指定的曲线内,只要有一个在就返回TRUE { CCurveEx curve; CCurveEx* pCurve; if(pOne->GetType()!=DOUBLEFOX_CURVE) { CRect8 range=pOne->GetRect(); curve.CreateCurveFromRect(&range); curve.m_type=PLINE_SOLID; pCurve=&curve; } else pCurve=(CCurveEx*)pOne->GetValue(); BOOL bInRgn=FALSE; COne* pRgnOne; CCurveEx* pRgnCurve; POSITION pos, pt; pos=rgnList.GetHeadPosition(); while(pos) { pt=rgnList.GetNext(pos); pRgnOne=GetDoc()->GetDraw()->GetAt(pt); pRgnCurve=(CCurveEx*)pRgnOne->GetValue(); if(CurveInRegion(pCurve, pRgnCurve)!=0) { bInRgn=TRUE; break; } } return bInRgn; } int CItemDelete::GetCountSelected() { return m_selectionSet.GetSize(); } BOOL CItemDelete::CurveInRegion(CCurveEx* pCurve, CPositionList& rgnList) //曲线是否在指定的元素内,只要有一个在就返回TRUE { BOOL br=FALSE; COne* pRgnOne; CCurveEx* pRgnCurve; POSITION pos, pt; pos=rgnList.GetHeadPosition(); while(pos) { pt=rgnList.GetNext(pos); pRgnOne=GetDoc()->GetDraw()->GetAt(pt); if(pRgnOne->GetType()!=DOUBLEFOX_CURVE) continue; pRgnCurve=(CCurveEx*)pRgnOne->GetValue(); if(CurveInRegion(pCurve, pRgnCurve)==1)//完全在曲线范围内部 { br=TRUE; break; } } return br; } //0为全不在范围内,1为全在范围内,2为部分在部分不在范围内 int CItemDelete::CurveInRegion(CCurveEx* pCurve, CCurveEx* pRgn) { ASSERT(pCurve); ASSERT(pRgn); CCrossList cross; pCurve->Cross(*(CCurve*)pRgn,cross); CPoint3D point(pCurve->x[0], pCurve->y[0], 0); if(cross.GetCount()==1) //为了删除打断后的曲线,如果被删除的曲线的端点正好落在范围线上时 { CCrossPoint pt=cross.GetHead(); if(pCurve->l[0]l[pCurve->num-1]) { if( fabs(pt.x[0]-pCurve->l[0])<1e-10 || fabs(pCurve->l[pCurve->num-1]-pt.x[0])<1e-10 ) cross.RemoveAll(); } else { if( fabs(pCurve->l[0]-pt.x[0])<1e-10 || fabs(pt.x[0]-pCurve->l[pCurve->num-1])<1e-10 ) cross.RemoveAll(); } if(cross.IsEmpty()) pCurve->GetCoordinate((pCurve->l[0]+pCurve->l[pCurve->num-1])*0.5, point.x0, point.y0, point.z0); //计算曲线中点 } else if(cross.GetCount()==2) { CCrossPoint head=cross.GetHead(); CCrossPoint tail=cross.GetTail(); if(pCurve->l[0]l[pCurve->num-1]) { if( fabs((pCurve->l[pCurve->num-1]-pCurve->l[0]) - (tail.x[0]-head.x[0]))<1e-10) cross.RemoveAll(); } else { if( fabs((pCurve->l[0]-pCurve->l[pCurve->num-1]) - (tail.x[0]-head.x[0]))<1e-10) cross.RemoveAll(); } if(cross.IsEmpty()) pCurve->GetCoordinate((pCurve->l[0]+pCurve->l[pCurve->num-1])*0.5, point.x0, point.y0, point.z0); //计算曲线中点 } if(cross.IsEmpty()) { if(pRgn->IsInside(point.x0, point.y0)) return 1; else return 0; } return 2; } int CItemDelete::DeleteCondition(DeletionCondition * delCondition) { if (delCondition == NULL) { return 0; } //CPropertiesSheet sheet(IDS_GI_DELETE_CONDITION,GetView(),0,FALSE); //sheet.SetItem(this); //if(sheet.DoModal()!=IDOK) return 0; CRect8 rect=GetRect(); CList list; GetDoc()->GetDraw()->IsInRange(rect,list); if(list.IsEmpty()) return 0; sel = delCondition->kind; value = delCondition->threshold; condition = delCondition->ConditionDeletion; mode = delCondition->method; switch(sel) { default: case 0: DeleteSelectWidthLength(list, value, condition); break; case 1: case 2: DeleteSelectWidthName(list, value, condition, sel); break; } if(list.IsEmpty()) return 0; CLayer *pLayer=NULL; if(mode==1)//移到规定的层 { pLayer=GetDoc()->GetDraw()->FindAddLayer(strLayer); //更新层树与工具条列表 //CMainFrame* pmf = (CMainFrame*)GetDoc()->GetMainFrame(); //if(pmf) // pmf->AddLayer(strLayer, pLayer); POSITION pos,pt; pos=list.GetHeadPosition(); while(pos) { pt=list.GetNext(pos); GetDoc()->GetDraw()->SetElementLayer(pt,pLayer); } } else { //GetDoc()->InvalidateDelete(list); if (list.IsEmpty()) return 0; CActionDeleteItem * pAction = new CActionDeleteItem(GetDoc(), IDS_STRING_ACTION_DELETE, list); GetDoc()->SetActionItem(pAction); list.RemoveAll(); //m_pSelectCurve->RemoveAllSelect(); } return 1; } void CItemDelete::DeleteSelectWidthName(CList &list, double value, int flag, int type) { CString name; int et; CCurveEx* pCurve; CPointNameEx* pPoint; POSITION p,pos,pt; p=list.GetHeadPosition(); while(p) { pos=p; pt=list.GetNext(p); name.Empty(); et=GetDoc()->GetDraw()->GetElementType(pt); switch(type) { case 1: if(et!=DOUBLEFOX_CURVE) list.RemoveAt(pos); else { pCurve=(CCurveEx*)GetDoc()->GetDraw()->GetAtValue(pt); name=pCurve->GetName(); } break; case 2: if(et!=DOUBLEFOX_POINT && et != DOUBLEFOX_XYZ) list.RemoveAt(pos); else { pPoint=(CPointNameEx*)GetDoc()->GetDraw()->GetAtValue(pt); name=pPoint->GetName(); } break; } if(name.IsEmpty())continue; if(!DeleteQualification(atof(name),value,flag)) list.RemoveAt(pos); } } void CItemDelete::DeleteSelectWidthLength(CList &list, double value, int flag) { CCurveEx* pCurve; POSITION p,pos,pt; p=list.GetHeadPosition(); while(p) { pos=p; pt=list.GetNext(p); if(GetDoc()->GetDraw()->GetElementType(pt)!=DOUBLEFOX_CURVE) list.RemoveAt(pos); else { pCurve=(CCurveEx*)GetDoc()->GetDraw()->GetAtValue(pt); //if(DeleteQualification(pCurve->Length(),value,flag)) if (!DeleteQualification(pCurve->Length(), value, flag)) list.RemoveAt(pos); } } } BOOL CItemDelete::DeleteQualification(double length, double value, int flag) { BOOL bDelete=FALSE; switch(flag) { case 0: //< if(length= if(length>=value)bDelete=TRUE; break; case 4: //> if(length>value)bDelete=TRUE; break; } return bDelete; } int CItemDelete::GetProcessIdea(void) { return m_idea; } void CItemDelete::OnRButtonDown(UINT nFlags, CPoint point) { if(m_idea==DELETE_DEFAULT || m_idea==DELETE_IN) GetDoc()->DeleteItem(); //GetDoc()->EnableDefaultTool(); } void CItemDelete::DeleteSelection(CList * pList) { if (pList->IsEmpty()) return ; CActionDeleteItem deleteItem(GetDoc(), IDS_STRING_ACTION_DELETE, *pList); deleteItem.Do(); pList->RemoveAll(); }