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