ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/REPOS_ERICCA/magic/lib/geometrie/src/sld_fonction.cpp
Revision: 22
Committed: Wed Jul 4 21:15:21 2007 UTC (17 years, 10 months ago) by bournival
Original Path: magic/lib/geometrie/geometrie/src/sld_fonction.cpp
File size: 17104 byte(s)
Log Message:

File Contents

# User Rev Content
1 5 //------------------------------------------------------------
2     //------------------------------------------------------------
3     // MAGiC
4     // Jean Christophe Cuillière et Vincent FRANCOIS
5     // Département de Génie Mécanique - UQTR
6     //------------------------------------------------------------
7     // Le projet MAGIC est un projet de recherche du département
8     // de génie mécanique de l'Université du Québec à
9     // Trois Rivières
10     // Les librairies ne peuvent être utilisées sans l'accord
11     // des auteurs (contact : francois@uqtr.ca)
12     //------------------------------------------------------------
13     //------------------------------------------------------------
14     //
15     // sld_fonction.cpp
16     //
17     //------------------------------------------------------------
18     //------------------------------------------------------------
19     // COPYRIGHT 2000
20     // Version du 02/03/2006 à 11H22
21     //------------------------------------------------------------
22     //------------------------------------------------------------
23    
24    
25     #include "gestionversion.h"
26     #ifdef BREP_SLD
27     //#include <atl\atlmod.h>
28     #include "smartvars.h"
29     //#include <clx.h>
30     //#include "sldworks_tlb.h"
31     //#include "swconst_tlb.h"
32    
33     #include <iostream>
34    
35     #include <system.hpp>
36     #include "sld_fonction.h"
37     #include <math.h>
38    
39     using namespace std;
40    
41     SLD_FONCTION::SLD_FONCTION():ouvert(0)
42     {
43 bournival 22 tolerance = 0;
44 5 }
45    
46     SLD_FONCTION::~SLD_FONCTION()
47     {
48     }
49    
50    
51     void SLD_FONCTION::OuvrirFichier(char* file)
52     {
53     WideString acstostr(file);
54     BSTR filename=BSTR(acstostr);
55     WideString acstostr2("");
56     BSTR configuration=BSTR(acstostr2);
57    
58     long type ;
59     type=1 ;
60     long options ;
61     options= 1;
62    
63     long Errors;
64     long Warnings;
65     swApp->OpenDoc6(filename, type, options, configuration, &Errors, &Warnings, &swModel);
66     return ;
67    
68     }
69    
70    
71    
72     void SLD_FONCTION::get_face_tessellation(CComPtr<IFace2> __swFace, long *__triangleCount, double ** __tessPts)
73     {
74     HRESULT hr;
75     hr = __swFace->GetTessTriangleCount(__triangleCount);
76    
77     CComVariant vTessTriang;
78     hr = __swFace->GetTessTriangles(VARIANT_TRUE, &vTessTriang);
79     SafeFloatArray saTessPts (vTessTriang);
80    
81     unsigned long nbCoords = *__triangleCount*9;
82     *__tessPts = new double [nbCoords];
83    
84     for (int i=0; i < nbCoords; i++)
85     {
86     (*__tessPts)[i] = saTessPts[i];
87     }
88     }
89    
90    
91     void SLD_FONCTION::Sauvegarder(char* file)
92     {
93     short retval =-1;
94     long Errors = -1;
95     long Warnings=-1;
96    
97     WideString acstostr(file);
98     BSTR nom=BSTR(acstostr);
99     swModel->SaveAs4 ( nom, swSaveAsCurrentVersion, swSaveAsOptions_SaveReferenced , &Errors, &Warnings ,&retval);
100     //if (retval==0 || hres !=S_OK)
101     //cout<<"Sauvegarde échouée!"<<endl;
102     return;
103     }
104    
105    
106    
107    
108    
109    
110    
111     void SLD_FONCTION::Connection(void)
112     {
113     //HRESULT hres;
114     CoInitialize(NULL); //Initialize COM
115     swApp.CoCreateInstance(CLSID_SldWorks_, NULL, CLSCTX_LOCAL_SERVER); //Create an instance of SolidWorks
116     //HRESULT status =swApp->put_Visible( true );
117     ouvert=1;
118     return;
119     }
120    
121    
122    
123    
124    
125    
126    
127     void SLD_FONCTION::Fermer(void)
128     {
129     if (!ouvert) return;
130     swApp->ExitApp();
131     swApp = NULL;
132     CoUninitialize();
133     return;
134     }
135    
136    
137    
138    
139    
140    
141    
142     // ********************************************************************
143     // ******** Procédure qui met un nom sur les entités de solidworks ****
144     // ********************************************************************
145    
146     void SLD_FONCTION::identificateur(void)
147     {
148     VideNom(); // procédure qui s'assure que toutes les entités sont vides (n'ont pas de nom)
149     CComQIPtr <IPartDoc> swPart;
150     swPart = swModel;
151     CComVariant vBodyArr; // si plus d'un corps dans le fichier part
152     swPart->GetBodies2(swSolidBody, VARIANT_TRUE, &vBodyArr);
153     SAFEARRAY* psaBody = V_ARRAY(&vBodyArr);
154     LPDISPATCH* pBodyDispArray = NULL;
155     long nBodyHighIndex = -1;
156     long nBodyCount = -1;
157     SafeArrayAccessData(psaBody, (void **) &pBodyDispArray);
158     SafeArrayGetUBound(psaBody, 1, &nBodyHighIndex);
159     nBodyCount = nBodyHighIndex + 1;
160     for (int i = 0; i < nBodyCount; i++)
161     {
162     CComQIPtr <IBody2> pBody;
163     pBody = pBodyDispArray[i];
164     CComPtr <IFace2> face;
165     CComPtr <IFace2> suivantface;
166     CComPtr <ISurface> surf;
167     long nbFaces;
168     pBody->GetFaceCount(&nbFaces);
169     pBody->IGetFirstFace(&face);
170    
171     for(int f=0;f<nbFaces;f++)// boucle sur les faces
172     {
173     SetID(face.p,0);
174     CComPtr <ILoop2> loope;
175     CComPtr <ILoop2> nextloope;
176     face->IGetFirstLoop(&loope);
177     long nbloop = -1;
178     face->GetLoopCount(&nbloop);
179     for (int l = 0;l<nbloop;l++)
180     {
181     // maintenant les aretes
182     CComPtr <ICoEdge> Coarete;
183     CComPtr <ICoEdge> nextCoarete;
184     loope->IGetFirstCoEdge(&Coarete);
185     long nbCoarete = -1;
186     loope->GetEdgeCount(&nbCoarete);
187     for (int coa=0;coa<nbCoarete;coa++)
188     {
189     CComPtr <IEdge> arete;
190     Coarete->IGetEdge(&arete);
191     SetID(arete.p,0); // numérotation de l'arrète.
192     CComPtr <IVertex> Sommet1;
193     arete->IGetStartVertex(&Sommet1);
194    
195     if (Sommet1!=NULL)
196     {
197     SetID(Sommet1.p,0);
198     CComPtr <IVertex> Sommet2;
199     arete->IGetEndVertex(&Sommet2);
200     SetID(Sommet2.p,0);
201     Sommet2.Release();
202     }
203     arete.Release();
204     Sommet1.Release();
205     Coarete->IGetNext(&nextCoarete);
206     Coarete = nextCoarete;
207     nextCoarete.Release();
208     } // next CoEdge
209     Coarete.Release();
210     loope->IGetNext(&nextloope);
211     loope = nextloope;
212     nextloope.Release();
213    
214     } // next loop
215     loope.Release();
216     face->IGetNextFace(&suivantface);
217     face = suivantface;
218     suivantface.Release();
219     } // next face
220     face.Release();
221     pBody.Release();
222     } // next body
223     return;
224     }
225    
226    
227    
228    
229    
230    
231    
232    
233    
234    
235    
236    
237    
238    
239    
240     //******************************************************************
241     void SLD_FONCTION::VideNom(void)
242     {
243     CComQIPtr <IPartDoc> swPart;
244     swPart = swModel;
245     long count;
246     CComPtr <IEntity> entite;
247    
248     swPart->GetNamedEntitiesCount ( &count );
249     if (count==0)
250     return;
251    
252     CComVariant vBodyArr; // si plus d'un corps dans le fichier part
253     swPart->GetBodies2(swSolidBody, VARIANT_TRUE, &vBodyArr);
254     SAFEARRAY* psaBody = V_ARRAY(&vBodyArr);
255     LPDISPATCH* pBodyDispArray = NULL;
256     long nBodyHighIndex = -1;
257     long nBodyCount = -1;
258     SafeArrayAccessData(psaBody, (void **) &pBodyDispArray);
259     SafeArrayGetUBound(psaBody, 1, &nBodyHighIndex);
260     nBodyCount = nBodyHighIndex + 1;
261    
262     for (int i = 0; i < nBodyCount; i++)
263     {
264     CComQIPtr <IBody2> pBody;
265     pBody = pBodyDispArray[i];
266     CComPtr <IFace2> face;
267     CComPtr <IFace2> suivantface;
268     CComPtr <ISurface> surf;
269     long nbFaces;
270     pBody->GetFaceCount(&nbFaces);
271     pBody->IGetFirstFace(&face);
272    
273     for(int f=0;f<nbFaces;f++)// boucle sur les faces
274     {
275     EffaceID(face);
276    
277     CComPtr <ILoop2> loope;
278     CComPtr <ILoop2> nextloope;
279     face->IGetFirstLoop(&loope);
280     long nbloop = -1;
281     face->GetLoopCount(&nbloop);
282     for (int l = 0;l<nbloop;l++)
283     {
284     // maintenant les aretes
285     CComPtr <ICoEdge> Coarete;
286     CComPtr <ICoEdge> nextCoarete;
287     loope->IGetFirstCoEdge(&Coarete);
288     long nbCoarete = -1;
289     loope->GetEdgeCount(&nbCoarete);
290     for (int coa=0;coa<nbCoarete;coa++)
291     {
292    
293     CComPtr <IEdge> arete;
294     Coarete->IGetEdge(&arete);
295    
296     EffaceID(arete.p);
297    
298     CComPtr <IVertex> Sommet1;
299     arete->IGetStartVertex(&Sommet1);
300    
301     if (Sommet1!=NULL)
302     {
303     EffaceID(Sommet1.p);
304     CComPtr <IVertex> Sommet2;
305     arete->IGetEndVertex(&Sommet2);
306     EffaceID(Sommet2.p);
307     Sommet2.Release();
308     }
309    
310     arete.Release();
311     Sommet1.Release();
312    
313     Coarete->IGetNext(&nextCoarete);
314     Coarete = nextCoarete;
315     nextCoarete.Release();
316    
317     } // next CoEdge
318     Coarete.Release();
319    
320     loope->IGetNext(&nextloope);
321     loope = nextloope;
322     nextloope.Release();
323     } // next loop
324    
325     loope.Release();
326     face->IGetNextFace(&suivantface);
327     face = suivantface;
328     suivantface.Release();
329     } // next face
330     face.Release();
331     pBody.Release();
332     } // next body
333    
334     return;
335     }
336    
337    
338    
339    
340     //*****************************************
341     BOOL SLD_FONCTION::GetParID(char* ID, CComPtr<IFace2> &face )
342     {
343     CComPtr <IEntity> ent;
344     WideString acstostr(ID);
345     BSTR nom=BSTR(acstostr);
346     CComQIPtr <IPartDoc> swPart;
347     swPart = swModel;
348     swPart->IGetEntityByName ( nom, swSelFACES , &ent ) ;
349     ent->QueryInterface( IID_IFace2, (LPVOID*)&face);
350    
351     return true;
352     }
353    
354     BOOL SLD_FONCTION::GetParID(BSTR nom, CComPtr<IFace2> &face )
355     {
356     CComPtr <IEntity> ent;
357     CComQIPtr <IPartDoc> swPart;
358     swPart = swModel;
359     swPart->IGetEntityByName ( nom, swSelFACES , &ent ) ;
360     ent->QueryInterface( IID_IFace2, (LPVOID*)&face);
361    
362     return true;
363     }
364    
365     //*****************************************
366     BOOL SLD_FONCTION::GetParID(char* ID,CComPtr<IEdge> &arete)
367     {
368     CComPtr <IEntity> ent;
369     WideString acstostr(ID);
370     BSTR nom=BSTR(acstostr);
371     CComQIPtr <IPartDoc> swPart;
372     swPart = swModel;
373     swPart->IGetEntityByName ( nom, swSelEDGES , &ent ) ;
374     ent->QueryInterface( IID_IEdge, (LPVOID*)&arete);
375     return true;
376     }
377    
378     BOOL SLD_FONCTION::GetParID(BSTR nom,CComPtr<IEdge> &arete)
379     {
380     CComPtr <IEntity> ent;
381     CComQIPtr <IPartDoc> swPart;
382     swPart = swModel;
383     swPart->IGetEntityByName ( nom, swSelEDGES , &ent ) ;
384     ent->QueryInterface( IID_IEdge, (LPVOID*)&arete);
385     return true;
386     }
387    
388     //*****************************************
389     BOOL SLD_FONCTION::GetParID(char* ID,CComPtr<IVertex> &sommet)
390     {
391     CComPtr <IEntity> ent;
392     WideString acstostr(ID);
393     BSTR nom=BSTR(acstostr);
394     CComQIPtr <IPartDoc> swPart;
395     swPart = swModel;
396     swPart->IGetEntityByName ( nom, swSelVERTICES , &ent ) ;
397     ent->QueryInterface( IID_IVertex, (LPVOID*)&sommet);
398     return true;
399     }
400    
401     BOOL SLD_FONCTION::GetParID(BSTR nom,CComPtr<IVertex> &sommet)
402     {
403     CComPtr <IEntity> ent;
404     CComQIPtr <IPartDoc> swPart;
405     swPart = swModel;
406     swPart->IGetEntityByName ( nom, swSelVERTICES , &ent ) ;
407     ent->QueryInterface( IID_IVertex, (LPVOID*)&sommet);
408     return true;
409     }
410    
411     //********************************************************
412     BOOL SLD_FONCTION::GetParID(char* ID, CComPtr<ISurface> &surface)
413     {
414     CComPtr<IFace2> face;
415     GetParID( ID,face); // on va chercher le pointeur pour la face
416     face->IGetSurface(&surface);
417     return true;
418     }
419    
420     //********************************************************
421     BOOL SLD_FONCTION::GetParID(char* ID,CComPtr<ICurve> &courbe)
422     {
423     CComPtr<IEdge> arete;
424     GetParID(ID,arete );
425     arete->IGetCurve(&courbe);
426     return true;
427     }
428    
429    
430    
431    
432     //********************************************************
433     // **** place l'ID de la Face dans solidworks ****
434     // *******************************************************
435     void SLD_FONCTION::SetID(IFace2 *face, long type )
436     {
437    
438     static long compteur=1;
439     CComPtr <IEntity> ent;
440     short retval; //et la doc qui dit que ce doit être un booléen ?!?
441    
442     char nom2[20];
443    
444     if( type==0 )// face normale apartient à un volume
445     strcpy(nom2,"Face");
446     else // face appartenant à une coque
447     strcpy(nom2,"Face_Libre");
448    
449     char nom3[10];
450     _ltoa(compteur,nom3,10);
451     strcat(nom2,nom3);
452    
453     WideString acstostr(nom2);
454     BSTR nom=BSTR(acstostr);
455    
456     face->QueryInterface( IID_IEntity, (LPVOID*)&ent);
457     CComQIPtr <IPartDoc> swPart;
458     swPart = swModel;
459     swPart->ISetEntityName( ent, nom , &retval );
460     if (retval!=0) compteur++;
461    
462     }
463    
464    
465     void SLD_FONCTION::EffaceID(IFace2 *face)
466     {
467     CComPtr <IEntity> ent;
468     short retval;
469     face->QueryInterface( IID_IEntity, (LPVOID*)&ent);
470     CComQIPtr <IPartDoc> swPart;
471     swPart = swModel;
472     swPart->DeleteEntityName ( ent, &retval );
473    
474     }
475    
476    
477     //********************************************************
478     void SLD_FONCTION::SetID(IEdge *arete,long type )
479     {
480    
481     static long compteur=1;
482     CComPtr <IEntity> ent;
483     short retval; //et la doc qui dit que ce doit être un booléen ?!?
484    
485     char nom2[20];
486    
487     if( type==0 ) // arete apartient à un volume
488     strcpy(nom2,"Arete");
489     else if (type==1) // arete appartenant à une coque
490     strcpy(nom2,"Arete_Coque");
491     else // arete seule (poutre)
492     strcpy(nom2,"Arete_Poutre");
493    
494     char nom3[10];
495     _ltoa(compteur,nom3,10);
496     strcat(nom2,nom3);
497    
498     WideString acstostr(nom2);
499     BSTR nom=BSTR(acstostr);
500    
501     arete->QueryInterface( IID_IEntity, (LPVOID*)&ent);
502     CComQIPtr <IPartDoc> swPart;
503     swPart = swModel;
504     swPart->ISetEntityName( ent, nom , &retval );
505    
506     if (retval!=0) compteur++;
507    
508     }
509    
510    
511    
512     void SLD_FONCTION::EffaceID(IEdge* arete)
513     {
514     CComPtr <IEntity> ent;
515     short retval;
516     arete->QueryInterface( IID_IEntity, (LPVOID*)&ent);
517     CComQIPtr <IPartDoc> swPart;
518     swPart = swModel;
519     swPart->DeleteEntityName ( ent, &retval );
520    
521     }
522    
523    
524     //********************************************************
525     void SLD_FONCTION::SetID(IVertex* sommet,long type )
526     {
527    
528     static long compteur=1;
529     CComPtr <IEntity> ent;
530     short retval; //et la doc qui dit que ce doit être un booléen ?!?
531    
532     char nom2[20];
533    
534     if( type==0 ) // sommet d'un volume
535     strcpy(nom2,"Sommet");
536     else if (type==1) // sommet d'une coque
537     strcpy(nom2,"Sommet_Coque");
538     else // sommet d'une poutre
539     strcpy(nom2,"Sommet_Poutre");
540    
541     char nom3[10];
542     _ltoa(compteur,nom3,10);
543     strcat(nom2,nom3);
544    
545     WideString acstostr(nom2);
546     BSTR nom=BSTR(acstostr);
547    
548     sommet->QueryInterface( IID_IEntity, (LPVOID*)&ent);
549     CComQIPtr <IPartDoc> swPart;
550     swPart = swModel;
551     swPart->ISetEntityName( ent, nom , &retval );
552    
553     if (retval!=0) compteur++;
554    
555     }
556    
557     void SLD_FONCTION::EffaceID(IVertex* sommet)
558     {
559     CComPtr <IEntity> ent;
560     short retval;
561     sommet->QueryInterface( IID_IEntity, (LPVOID*)&ent);
562     CComQIPtr <IPartDoc> swPart;
563     swPart = swModel;
564     swPart->DeleteEntityName ( ent, &retval );
565    
566     }
567    
568    
569     //*********************************************************
570     // **** Procédure qui renvoie le ID d'une arete **********
571     //*********************************************************
572     AnsiString SLD_FONCTION::GetID(IEdge* arete)
573     {
574    
575     CComPtr <IEntity> ent;
576     CComPtr <IEdge> swarete(arete);
577     swarete->QueryInterface( IID_IEntity, (LPVOID*)&ent);
578    
579     BSTR retval;
580     swModel->IGetEntityName ( ent, &retval );
581    
582 bournival 22
583     // on vérifie, si c'est un esclave on retourne le nom du maitre.
584     CComPtr <IAttribute> Attribut;
585     ent->IFindAttribute ( DefAttrDoublon, 0, &Attribut ) ;
586     if(Attribut!=NULL)
587     {
588     IParameter* parammaitre;
589     CComBSTR nomParam("Maitre.") ;
590     Attribut->IGetParameter ( nomParam, &parammaitre );
591     parammaitre->GetStringValue(&retval) ;
592     }
593     else
594     {
595     swModel->IGetEntityName ( ent, &retval );
596     }
597    
598 5 AnsiString rep(retval);
599     return (rep);
600    
601     }
602    
603     //******************************************************
604     AnsiString SLD_FONCTION::GetID(IFace2* face)
605     {
606    
607     CComPtr <IEntity> ent;
608     CComPtr <IFace2> swface(face);
609    
610     swface->QueryInterface( IID_IEntity, (LPVOID*)&ent);
611    
612     BSTR retval;
613     swModel->IGetEntityName ( ent, &retval );
614    
615     AnsiString rep(retval);
616     return (rep);
617    
618     }
619    
620    
621     //****************************
622     AnsiString SLD_FONCTION::GetID(IVertex* sommet )
623     {
624     CComPtr <IEntity> ent;
625     CComPtr<IVertex> swSommet(sommet);
626     swSommet->QueryInterface( IID_IEntity, (LPVOID*)&ent);
627     BSTR retval;
628    
629    
630     // on vérifie, si c'est un esclave on retourne le nom du maitre.
631    
632     CComPtr <IAttribute> Attribut;
633     ent->IFindAttribute ( DefAttrDoublon, 0, &Attribut ) ;
634     if(Attribut!=NULL)
635     {
636     IParameter* parammaitre;
637     CComBSTR nomParam("Maitre.") ;
638     Attribut->IGetParameter ( nomParam, &parammaitre );
639     parammaitre->GetStringValue(&retval) ;
640     }
641     else
642     {
643     swModel->IGetEntityName ( ent, &retval );
644     }
645    
646     AnsiString rep(retval);
647     return (rep);
648    
649     }
650    
651    
652 bournival 22 double SLD_FONCTION::GetValeurTolerance()
653     {
654     if(tolerance!=0) return tolerance; // pour ne pas refaire le calcul plusieurs fois pour avoir la même valeur.
655     double petit = 999999999;
656     double actuelle = 0;
657 5
658 bournival 22 // on demande le body au model
659     CComVariant vBodyArr; // si plus d'un corps dans le fichier part
660     CComQIPtr<IPartDoc> swPart;
661     swPart = swModel;
662     swPart->GetBodies2(swSolidBody, VARIANT_TRUE, &vBodyArr);
663     SAFEARRAY* psaBody = V_ARRAY(&vBodyArr);
664     LPDISPATCH* pBodyDispArray = NULL;
665     long nBodyHighIndex = -1;
666     SafeArrayAccessData(psaBody, (void **) &pBodyDispArray);
667     SafeArrayGetUBound(psaBody, 1, &nBodyHighIndex);
668     long nb_sld_corps = nBodyHighIndex + 1;
669 5
670 bournival 22 for (long i=0;i<nb_sld_corps;i++) // boucle sur les corps
671     {
672     CComQIPtr <IBody2> swBody;
673     swBody = pBodyDispArray[i];
674     double coins[6];
675     CComVariant vRetval;
676     vRetval=swBody->GetBodyBox();
677     SafeDoubleArray sda(vRetval);
678     //HRESULT status = swBody->IGetBodyBox ( coins );
679     coins[0]=sda[0];
680     coins[1]=sda[1];
681     coins[2]=sda[2];
682     coins[3]=sda[3];
683     coins[4]=sda[4];
684     coins[5]=sda[5];
685     actuelle = sqrt( (coins[0] - coins[3])*(coins[0] - coins[3]) + (coins[1] - coins[4])*(coins[1] - coins[4]) + (coins[2] - coins[5])*(coins[2] - coins[5])) /10000000 ;
686     if (actuelle <petit ) petit = actuelle;
687     }
688     tolerance = petit;
689 5
690 bournival 22 return tolerance;
691     }
692    
693    
694 5 #endif
695    
696