eaca832d19f017e0479b02d4924ee6c4ac6b8350
[occt.git] / samples / mfc / standard / 05_ImportExport / src / ImportExportDoc.cpp
1 // ImportExportDoc.cpp : implementation of the CImportExportDoc class
2 //
3
4
5 #include "stdafx.h"
6 #include "ImportExportApp.h"
7
8 #include "ImportExportDoc.h"
9
10 #include <ImportExport/ImportExport.h>
11
12 #include <AISDialogs.h>
13 #include "res/resource.h"
14
15
16 #ifdef _DEBUG
17 //#define new DEBUG_NEW  // by cascade
18 #undef THIS_FILE
19 static char THIS_FILE[] = __FILE__;
20 #endif
21
22 /////////////////////////////////////////////////////////////////////////////
23 // CImportExportDoc
24
25 IMPLEMENT_DYNCREATE(CImportExportDoc, OCC_3dDoc)
26
27 BEGIN_MESSAGE_MAP(CImportExportDoc, OCC_3dDoc)
28         //{{AFX_MSG_MAP(CImportExportDoc)
29         ON_COMMAND(ID_FILE_IMPORT_BREP, OnFileImportBrep)
30         ON_COMMAND(ID_FILE_IMPORT_IGES, OnFileImportIges)
31         ON_COMMAND(ID_FILE_EXPORT_IGES, OnFileExportIges)
32         ON_COMMAND(ID_FILE_IMPORT_STEP, OnFileImportStep)
33         ON_COMMAND(ID_FILE_EXPORT_STEP, OnFileExportStep)
34         ON_COMMAND(ID_FILE_EXPORT_VRML, OnFileExportVrml)
35         ON_COMMAND(ID_FILE_EXPORT_STL, OnFileExportStl)
36         ON_COMMAND(ID_BOX, OnBox)
37         ON_COMMAND(ID_Cylinder, OnCylinder)
38         ON_COMMAND(ID_OBJECT_REMOVE, OnObjectRemove)
39         ON_COMMAND(ID_OBJECT_ERASE, OnObjectErase)
40         ON_COMMAND(ID_OBJECT_DISPLAYALL, OnObjectDisplayall)
41         //}}AFX_MSG_MAP
42
43 END_MESSAGE_MAP()
44
45 /////////////////////////////////////////////////////////////////////////////
46 // CImportExportDoc construction/destruction
47
48 CImportExportDoc::CImportExportDoc()
49 : OCC_3dDoc (false)
50 {
51 /*
52     // TRIHEDRON
53         Handle(AIS_Trihedron) aTrihedron;
54         Handle(Geom_Axis2Placement) aTrihedronAxis=new Geom_Axis2Placement(gp::XOY());
55         aTrihedron=new AIS_Trihedron(aTrihedronAxis);
56         myAISContext->Display(aTrihedron);
57 */
58
59         m_pcoloredshapeList = new CColoredShapes();
60 }
61
62 CImportExportDoc::~CImportExportDoc()
63 {
64         if( m_pcoloredshapeList ) delete m_pcoloredshapeList;
65 }
66
67
68 /////////////////////////////////////////////////////////////////////////////
69 // CSerializeDoc serialization
70
71 void CImportExportDoc::Serialize(CArchive& ar)
72 {
73         if (ar.IsStoring())
74         {
75                 // Put the current CColoredShape in the archive
76                 ar << m_pcoloredshapeList;
77         }
78         else
79         {
80                 // Read from the archive the current CColoredShape
81                 ar >> m_pcoloredshapeList;
82
83                 // Display the new object
84                 m_pcoloredshapeList->Display(myAISContext);
85         }
86 }
87
88
89 /*
90 void CImportExportDoc::OnWindowNew3d() 
91 {
92         ((CImportExportApp*)AfxGetApp())->CreateView3D(this);   
93 }
94 */
95
96 //  nCmdShow could be :    ( default is SW_RESTORE ) 
97 // SW_HIDE   SW_SHOWNORMAL   SW_NORMAL   
98 // SW_SHOWMINIMIZED     SW_SHOWMAXIMIZED    
99 // SW_MAXIMIZE          SW_SHOWNOACTIVATE   
100 // SW_SHOW              SW_MINIMIZE         
101 // SW_SHOWMINNOACTIVE   SW_SHOWNA           
102 // SW_RESTORE           SW_SHOWDEFAULT      
103 // SW_MAX    
104
105 // use pViewClass = RUNTIME_CLASS( CImportExportView3D ) for 3D Views
106
107 void CImportExportDoc::ActivateFrame(CRuntimeClass* pViewClass,int nCmdShow)
108 {
109   POSITION position = GetFirstViewPosition();
110   while (position != (POSITION)NULL)
111   {
112     CView* pCurrentView = (CView*)GetNextView(position);
113      if(pCurrentView->IsKindOf(pViewClass) )
114     {
115         ASSERT_VALID(pCurrentView);
116         CFrameWnd* pParentFrm = pCurrentView->GetParentFrame();
117             ASSERT(pParentFrm != (CFrameWnd *)NULL);
118         // simply make the frame window visible
119             pParentFrm->ActivateFrame(nCmdShow);
120     }
121   }
122
123 }
124
125 /////////////////////////////////////////////////////////////////////////////
126 // CImportExportDoc diagnostics
127
128 #ifdef _DEBUG
129 void CImportExportDoc::AssertValid() const
130 {
131         CDocument::AssertValid();
132 }
133
134 void CImportExportDoc::Dump(CDumpContext& dc) const
135 {
136         CDocument::Dump(dc);
137 }
138 #endif //_DEBUG
139
140 /////////////////////////////////////////////////////////////////////////////
141 // CImportExportDoc commands
142
143
144 void CImportExportDoc::OnFileImportBrep()
145 {
146         Handle(TopTools_HSequenceOfShape) aSeqOfShape = CImportExport::ReadBREP();
147         for(int i=1;i<= aSeqOfShape->Length();i++)
148         {
149                 m_pcoloredshapeList->Add(Quantity_NOC_YELLOW, aSeqOfShape->Value(i));
150         m_pcoloredshapeList->Display(myAISContext);
151         }
152         Fit();
153 }
154
155 void CImportExportDoc::OnFileImportIges() 
156 {   
157         Handle(TopTools_HSequenceOfShape) aSeqOfShape = CImportExport::ReadIGES();
158         for(int i=1;i<= aSeqOfShape->Length();i++)
159         {
160                 m_pcoloredshapeList->Add(Quantity_NOC_YELLOW, aSeqOfShape->Value(i));
161         m_pcoloredshapeList->Display(myAISContext);
162         }
163         Fit();
164 }
165 void CImportExportDoc::OnFileExportIges() 
166 {   CImportExport::SaveIGES(myAISContext);}
167
168 void CImportExportDoc::OnFileImportStep() 
169 {   
170         Handle(TopTools_HSequenceOfShape) aSeqOfShape = CImportExport::ReadSTEP();
171         for(int i=1;i<= aSeqOfShape->Length();i++)
172         {
173                 m_pcoloredshapeList->Add(Quantity_NOC_YELLOW, aSeqOfShape->Value(i));
174         m_pcoloredshapeList->Display(myAISContext);
175         }
176         Fit();
177 }
178 void CImportExportDoc::OnFileExportStep() 
179 {   CImportExport::SaveSTEP(myAISContext);}
180
181
182 void CImportExportDoc::OnFileExportVrml() 
183 {   CImportExport::SaveVRML(myAISContext);}
184
185 void CImportExportDoc::OnFileExportStl() 
186 {   CImportExport::SaveSTL(myAISContext);}
187
188 void  CImportExportDoc::Popup(const Standard_Integer  x,
189                                                            const Standard_Integer  y ,
190                                const Handle(V3d_View)& aView   ) 
191 {
192   Standard_Integer PopupMenuNumber=0;
193  myAISContext->InitSelected();
194   if (myAISContext->MoreSelected())
195     PopupMenuNumber=1;
196
197   CMenu menu;
198   VERIFY(menu.LoadMenu(IDR_Popup3D));
199   CMenu* pPopup = menu.GetSubMenu(PopupMenuNumber);
200
201   ASSERT(pPopup != NULL);
202    if (PopupMenuNumber == 1) // more than 1 object.
203   {
204     bool OneOrMoreInShading = false;
205         for (myAISContext->InitSelected();myAISContext->MoreSelected ();myAISContext->NextSelected ())
206     if (myAISContext->IsDisplayed(myAISContext->SelectedInteractive(),1)) OneOrMoreInShading=true;
207         if(!OneOrMoreInShading)
208         pPopup->EnableMenuItem(5, MF_BYPOSITION | MF_DISABLED | MF_GRAYED);
209    }
210
211   POINT winCoord = { x , y };
212   Handle(WNT_Window) aWNTWindow=
213   Handle(WNT_Window)::DownCast(aView->Window());
214   ClientToScreen ( (HWND)(aWNTWindow->HWindow()),&winCoord);
215   pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON , winCoord.x, winCoord.y , 
216                          AfxGetMainWnd());
217
218
219 }
220
221 void CImportExportDoc::OnBox() 
222 {
223         AIS_ListOfInteractive aList;
224         myAISContext->DisplayedObjects(aList);
225         AIS_ListIteratorOfListOfInteractive aListIterator;
226         for(aListIterator.Initialize(aList);aListIterator.More();aListIterator.Next()){
227                 myAISContext->Remove (aListIterator.Value(), Standard_False);
228         }
229
230         BRepPrimAPI_MakeBox B(200.,150.,100.);
231
232         m_pcoloredshapeList->Add(Quantity_NOC_YELLOW, B.Shape());
233
234         m_pcoloredshapeList->Display(myAISContext);
235         Fit();
236
237         // document has been modified
238         SetModifiedFlag(TRUE);
239 }
240
241 void CImportExportDoc::OnCylinder() 
242 {
243         AIS_ListOfInteractive aList;
244         myAISContext->DisplayedObjects(aList);
245         AIS_ListIteratorOfListOfInteractive aListIterator;
246         for(aListIterator.Initialize(aList);aListIterator.More();aListIterator.Next()){
247                 myAISContext->Remove (aListIterator.Value(), Standard_False);
248         }
249
250         BRepPrimAPI_MakeCylinder C(50.,200.);
251
252         m_pcoloredshapeList->Add(Quantity_NOC_GREEN, C.Shape());
253
254         m_pcoloredshapeList->Display(myAISContext);
255         Fit();
256
257         // document has been modified
258         SetModifiedFlag(TRUE);
259 }
260 void CImportExportDoc::OnObjectRemove() 
261
262 {
263         for(GetAISContext()->InitSelected();GetAISContext()->MoreSelected();GetAISContext()->NextSelected()) {
264                 Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast(GetAISContext()->SelectedInteractive());
265                 if(!aShape.IsNull()) {
266                         m_pcoloredshapeList->Remove(aShape->Shape());
267                 }
268         }
269         OCC_3dBaseDoc::OnObjectRemove();
270 }
271
272 void CImportExportDoc::OnObjectErase() 
273
274 {
275         for(GetAISContext()->InitSelected();GetAISContext()->MoreSelected();GetAISContext()->NextSelected()) {
276                 Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast(GetAISContext()->SelectedInteractive());
277                 if(!aShape.IsNull()) {
278                         m_pcoloredshapeList->Remove(aShape->Shape());
279                 }
280         }
281         OCC_3dBaseDoc::OnObjectErase(); 
282 }
283
284 void CImportExportDoc::OnObjectDisplayall() 
285
286 {
287         OCC_3dBaseDoc::OnObjectDisplayall(); 
288 }