/
SolidEdgeUtils(old).cs
1072 lines (1051 loc) · 32.8 KB
/
SolidEdgeUtils(old).cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms; // Per MessageBox()
using System.Runtime.InteropServices; // Funzioni di base (COM)
using SolidEdgeFramework; // Funzioni di base di Solid Edge
using SolidEdgeFrameworkSupport;
using System.Globalization; // Per CultureInfo
using System.IO; // Per DirectoryInfo
namespace Fred68.Tools.Grafica
{
class SolidEdgeUtils
{
class ProprietaSolidEdge // Classe interna per archiviare le proprieta`
{
string mNome; // Proprieta` pubbliche solo in lettura
public string Nome
{
get { return mNome; }
protected set { mNome = value; }
}
int mId;
public int Id
{
get { return mId; }
protected set { mId = value; }
}
dynamic mValore; // Come tipo Variant ('var' non utilizzabile in una classe)
public dynamic Valore // In sola lettura
{ // Scrittura concessa solo per alcuni tipi di dato (in costruttore o altri metodi)
get { return mValore; }
}
public ProprietaSolidEdge(string nome, System.String valore, int id)
{
Nome = nome;
Id = id;
mValore = valore;
}
public ProprietaSolidEdge(string nome, System.Int32 valore, int id)
{
Nome = nome;
Id = id;
mValore = valore;
}
public ProprietaSolidEdge(string nome, System.Double valore, int id)
{
Nome = nome;
Id = id;
mValore = valore;
}
public ProprietaSolidEdge(string nome, System.DateTime valore, int id)
{
Nome = nome;
Id = id;
mValore = valore;
}
}
// Variabili membro private legate a oggetti COM di Solid Edge
SolidEdgeFramework.Application application = null; // Istanza
Type type = null; // Tipo di applicazione
SolidEdgeFramework.SolidEdgeDocument document = null; // Documento
SEInstallDataLib.SEInstallData installData = null; // Versione di Solid Edge...
int builderNumber = 0;
DirectoryInfo installFolder;
CultureInfo cultureInfo;
int majorVersion = 0;
int minorVersion = 0;
int parasolidMajorVersion = 0;
int parasolidMinorVersion = 0;
Version parasolidVersion;
int servicePackVersion = 0;
Version version;
SolidEdgeDraft.ModelLinks modelLinks = null; // Dati nel draft
SolidEdgeDraft.ModelLink modelLink = null;
SolidEdgeDraft.DrawingViews drawingViews = null; // Viste del foglio attivo
SolidEdgeDraft.DrawingView drawingView = null;
// SolidEdgeDraft.PartsLists partsLists = null; ...Distinte nel draft
// SolidEdgeDraft.PartsList partsList = null;
List<string> listaModelli = null; // Lista dei modelli collegati al draft
List<string> listaDocAperti = null; // Lista dei documenti aperti
List<string> listaProprietaDati = null; // Nomi proprieta` con i 'dati'. La prima e` il codice di disegno
List<ProprietaSolidEdge> listaProprietaFile = null; // Lista delle proprieta` del file collegato
List<string> listaScale = null; // Lista delle scale
string fullnameDoc, fullpdfDoc, fulldwgDoc; // Nomi completi per salvataggio su file
public bool nomecodicicorrispondono;
public static string tipoDraft = "Draft";
public static string tipoAssembly = "Assembly";
public static string tipoPart = "Part";
public static string tipoSheetMetal = "SheetMetal";
public static string tipoSconosciuto = "Sconosciuto";
public static string tipoWeldment = "Weldment";
public static string tipoWeldmentAssembly = "WeldmentAssembly";
public static string pathSeparator = "\\"; // Separatore di path
public static string codeSeparator = "."; // Separatore di codice es.: 780.36.220 (.)
public static string revSeparator = "-"; // Separatore di revisione es.: 780.36.220-a (-)
public static int lastLength = 3; // Numero di caratteri dell'ultimo gruppo prima
// della revisione es.: 780.36.220a (3),
// usato se non c'e` separatore di revisione
public static string altSeparator = "!"; // Separatore alternate assembly
public static string pdfExt = ".pdf"; // Estensioni
public static string dwgExt = ".dwg";
public static string dftExt = ".dft";
public static string dwgShExt = "-f"; // Estensione nomi fogli dwg
//////////////////////////////////////////////////////
public SolidEdgeUtils() // Costruttore
{
listaModelli = new List<string>();
listaDocAperti = new List<string>();
listaProprietaDati = new List<string>();
listaProprietaFile = new List<ProprietaSolidEdge>();
listaScale = new List<string>();
fullnameDoc = "";
fullpdfDoc = "";
fulldwgDoc = "";
nomecodicicorrispondono = false;
}
public string AvviaSolidEdge() // Avvia SE, restituisce string con messaggio
{
string messaggio = ""; // Legge eventuale istanza attiva. Eccezione se nessuna istanza
try
{
application = (SolidEdgeFramework.Application)Marshal.GetActiveObject("SolidEdge.Application");
}
catch
{
application = null; // Genera eccezione se SE non e` avviato
}
if (application != null) // Se non e` attivo, lancia l'istanza
{
messaggio = "Istanza Solid Edge gia` in funzione";
}
else
{
try
{ // Prova avvio di Solid Edge
type = Type.GetTypeFromProgID("SolidEdge.Application"); // Legge il tipo di appl. associato a SE
application = (SolidEdgeFramework.Application)Activator.CreateInstance(type); // Lancia un'istanza
application.Visible = true;
messaggio = "Avviato";
}
catch (System.Exception ex) // Messaggio se eccezione
{
messaggio = "Eccezione: " + ex.Message;
}
finally // Pulitura, eseguita sempre
{
if (application != null)
{
Marshal.ReleaseComObject(application);
application = null;
}
}
}
return messaggio;
}
public string TipoDocumentoAttivo() // Restituisce string con il tipo di documento attivo
{
string tipoDoc = "";
try
{
application = (SolidEdgeFramework.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("SolidEdge.Application");
}
catch
{
tipoDoc = "Nessuna istanza attiva";
application = null;
}
if(application != null) // Se SE e` in funzione, cerca di leggere il tipo di documento attivo
{
try
{
document = (SolidEdgeFramework.SolidEdgeDocument)application.ActiveDocument;
switch (document.Type)
{
case DocumentTypeConstants.igAssemblyDocument:
tipoDoc = tipoAssembly;
break;
case DocumentTypeConstants.igDraftDocument:
tipoDoc = tipoDraft;
break;
case DocumentTypeConstants.igPartDocument:
tipoDoc = tipoPart;
break;
case DocumentTypeConstants.igSheetMetalDocument:
tipoDoc = tipoSheetMetal;
break;
case DocumentTypeConstants.igUnknownDocument:
tipoDoc = tipoSconosciuto;
break;
case DocumentTypeConstants.igWeldmentAssemblyDocument:
tipoDoc = tipoWeldmentAssembly;
break;
case DocumentTypeConstants.igWeldmentDocument:
tipoDoc = tipoWeldment;
break;
}
}
catch
{
tipoDoc = "Nessun documento attivo";
}
finally
{
if (document != null)
{
Marshal.ReleaseComObject(document);
document = null;
}
if (application != null)
{
Marshal.ReleaseComObject(application);
application = null;
}
}
}
return tipoDoc;
}
public string NomeDocumentoAttivo() // Restituisce il nome completo del doc attivo
{
string nomeDoc = "";
try
{
application = (SolidEdgeFramework.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("SolidEdge.Application");
}
catch
{
application = null;
}
if (application != null) // Se SE e` in funzione, cerca di leggere il tipo di documento attivo
{
try
{
document = (SolidEdgeFramework.SolidEdgeDocument)application.ActiveDocument;
nomeDoc = document.FullName;
}
catch
{
nomeDoc = "";
}
finally
{
if (document != null)
{
Marshal.ReleaseComObject(document);
document = null;
}
if (application != null)
{
Marshal.ReleaseComObject(application);
application = null;
}
}
}
return nomeDoc;
}
public string VersioneSolidEdgeEInstallata() // Restituisce string con i dati della versione
{
string versione = "";
try
{
installData = new SEInstallDataLib.SEInstallData();
builderNumber = installData.GetBuildNumber();
installFolder = new DirectoryInfo(installData.GetInstalledPath());
cultureInfo = new CultureInfo(installData.GetLanguageID());
majorVersion = installData.GetMajorVersion();
minorVersion = installData.GetMinorVersion();
parasolidMajorVersion = installData.GetParasolidMajorVersion();
parasolidMinorVersion = installData.GetParasolidMinorVersion();
parasolidVersion = new Version(installData.GetParasolidVersion());
servicePackVersion = installData.GetServicePackVersion();
version = new Version(installData.GetVersion());
versione = string.Format("Versione di Solid Edge : {1}\nVersione parasolid : {0}\nCartella di installazione : {2}", parasolidVersion.ToString(), version.ToString(), installFolder.FullName);
}
catch (System.Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
if (installData != null)
{
Marshal.ReleaseComObject(installData);
installData = null;
}
}
return versione;
}
public string CreaListaModelli() // Riempie la lista dei nomi file collegati e rest. string messaggio
{
string messaggio = ""; // Cancella i dati preesistenti
listaModelli.Clear();
try
{
application = (SolidEdgeFramework.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("SolidEdge.Application");
}
catch
{
messaggio = "Nessuna istanza attiva";
application = null;
}
try
{
document = (SolidEdgeFramework.SolidEdgeDocument)application.ActiveDocument;
if(document.Type == DocumentTypeConstants.igDraftDocument) // Se e` un draft...
{
int count, i; // ...estrae i documenti linkati
modelLinks = ((SolidEdgeDraft.DraftDocument)document).ModelLinks;
count = modelLinks.Count;
for (i = 0; i < count; i++) // Li inserisce in una lista
{
modelLink = modelLinks.Item(i + 1);
string nomeFile = modelLink.FileName; // Filename completo
int indx = nomeFile.IndexOf(altSeparator); // Elimina alternate assembly
if( indx != -1)
{
nomeFile = nomeFile.Remove(indx); // Tutto cio` dopo '!'
}
listaModelli.Add(nomeFile);
}
}
else
{
messaggio = "Non e` un draft";
}
}
catch (System.Exception ex) // Messaggio se eccezione
{
messaggio = "Eccezione: " + ex.Message + " + Nessun documento attivo";
}
finally
{
if (modelLinks != null)
{
foreach(SolidEdgeDraft.ModelLink modelLink in modelLinks)
{
Marshal.ReleaseComObject(modelLink);
}
}
if (modelLinks != null)
{
Marshal.ReleaseComObject(modelLinks);
modelLinks = null;
}
if (document != null)
{
Marshal.ReleaseComObject(document);
document = null;
}
if (application != null)
{
Marshal.ReleaseComObject(application);
application = null;
}
}
return messaggio;
}
public List<string> ListaModelli // Proprieta` read only
{
get {return listaModelli;}
}
public string CreaListaProprietaDaFile(string fullname) // Lista da doc collegato su discio (poi resta in read only)
{
string msg = "";
if(DocumentoAperto(fullname))
{
return("Documento aperto");
}
SolidEdgeFileProperties.PropertySets propertySets3D = null; // Oggetti proprieta` del 3D
SolidEdgeFileProperties.Properties properties3D = null;
FileInfo dis3D = new FileInfo(fullname); // Crea rif. al file
if (dis3D.Exists) // Se il file esiste
{
listaProprietaFile.Clear(); // Cancella la lista delle proprieta attuale
try
{
propertySets3D = new SolidEdgeFileProperties.PropertySets(); // Oggetto PropertySets
propertySets3D.Open(fullname, true); // Apre proprieta` file readonly (true)
properties3D = (SolidEdgeFileProperties.Properties)propertySets3D["Custom"]; // Custom Property Set
foreach (SolidEdgeFileProperties.Property pr in properties3D) // Legge le proprieta`
{
listaProprietaFile.Add(new ProprietaSolidEdge(pr.Name, pr.Value, pr.ID)); // e le mette in una lista
if (pr != null)
{
Marshal.ReleaseComObject(pr);
}
}
msg = "Lettura proprieta` completata";
}
catch (System.Exception ex)
{
msg += ex.Message + " in CreaListaProprietaDaFile()";
}
finally // ATTENZIONE: NON RILASCIA IL FILE 3D, che rimane read-only !
{
if (properties3D != null)
{
Marshal.ReleaseComObject(properties3D);
properties3D = null;
}
if (propertySets3D != null)
{
propertySets3D.Close(); // Chiude il file prima di rilasciare il COM
Marshal.ReleaseComObject(propertySets3D);
Marshal.FinalReleaseComObject(propertySets3D);
propertySets3D = null;
}
GC.Collect(); // Per evitare errore di share violation dopo rilascio dei COM ?
GC.WaitForPendingFinalizers(); // Ma il file resta aperto.
}
} // fine blocco if(dis3D.Exists)
else
{
msg = "File inesistente";
}
return msg;
}
public string CreaListaProprietaDaIstanzaSolidEdge(string fullname) // Lista da doc collegato aperto
{
string msg = "";
SolidEdgeFramework.SolidEdgeDocument activeDocument = null; // Documento attivo
SolidEdgeFramework.PropertySets propertySets = null; // Proprieta` del documento aperto (non del file)
SolidEdgeFramework.Properties properties = null;
//SolidEdgeFramework.Property property = null;
try
{
application = (SolidEdgeFramework.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("SolidEdge.Application");
}
catch
{
application = null;
}
if (application != null) // Se SE e` in funzione...
{
listaProprietaFile.Clear(); // Cancella la lista delle proprieta attuale
try
{
activeDocument = (SolidEdgeFramework.SolidEdgeDocument)application.ActiveDocument; // Salva il rif. al doc. attivo
if(AttivaDocumento(fullname)) // Attiva il documento aperto con il nome file richiesto...
{
msg += " - ";
application = (SolidEdgeFramework.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("SolidEdge.Application");
document = (SolidEdgeFramework.SolidEdgeDocument)application.ActiveDocument; // Ne legge i rif. alle proprieta`
propertySets = (SolidEdgeFramework.PropertySets)document.Properties;
properties = propertySets.Item("Custom");
foreach (SolidEdgeFramework.Property pr in properties) // Percorre le proprieta`
{
listaProprietaFile.Add(new ProprietaSolidEdge(pr.Name, pr.get_Value(), 0)); // e le mette in una lista
if (pr != null)
{
Marshal.ReleaseComObject(pr);
}
}
msg = "Lettura proprieta` completata";
}
}
catch (System.Exception ex)
{
msg += ex.Message + " in CreaListaProprietaDaIstanzaSolidEdge()";
}
finally
{
if (document != null)
{
Marshal.ReleaseComObject(document);
document = null;
}
if (application != null)
{
Marshal.ReleaseComObject(application);
application = null;
}
activeDocument.Activate(); // Ripristina il doc che era attivo all'inizio della funzione
}
}
return msg;
}
public string VediListaProprieta() // Crea una stringa con la lista delle proprieta`
{
string msg = string.Format("Trovate N. {0} proprieta` custom:\n",listaProprietaFile.Count);
foreach (ProprietaSolidEdge pr in listaProprietaFile) // Legge le proprieta`
{
msg += string.Format("ID = {0} : Name = {1} : Value = {2} : Type = {3}\n",
pr.Id,
pr.Nome,
pr.Valore,
pr.Valore.GetType());
}
msg += "\nProprieta` filtrate:\n";
foreach (ProprietaSolidEdge pr in listaProprietaFile) // Legge le proprieta`
{
if(listaProprietaDati.Contains(pr.Nome))
msg += string.Format("Name = {0} : Value = {1} : Type = {2}\n",
pr.Nome,
pr.Valore,
pr.Valore.GetType());
}
return msg;
}
public bool CreaListaDocumentiAperti() // Crea lista documenti aperti
{
bool ret = false;
try
{
application = (SolidEdgeFramework.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("SolidEdge.Application");
}
catch
{
application = null;
}
if (application != null) // Se SE e` in funzione, leggere i documenti aperti e li mette in una lista
{
listaDocAperti.Clear();
try
{
foreach(SolidEdgeFramework.SolidEdgeDocument doc in application.Documents)
{
listaDocAperti.Add(doc.FullName);
}
ret = true;
}
catch
{
ret = false;
}
finally
{
if (document != null)
{
Marshal.ReleaseComObject(document);
document = null;
}
if (application != null)
{
Marshal.ReleaseComObject(application);
application = null;
}
}
}
return ret;
}
public List<string> ListaDocAperti // Proprieta` read only
{
get { return listaDocAperti; }
}
public bool DocumentoAperto(string fullname) // Restituisce true se il doc indicato e` aperto nella sessione SE
{
bool ret = false;
try
{
application = (SolidEdgeFramework.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("SolidEdge.Application");
}
catch
{
application = null;
}
if (application != null) // Se SE e` in funzione, leggere i documenti aperti e li mette in una lista
{
try
{
foreach (SolidEdgeFramework.SolidEdgeDocument doc in application.Documents)
{
if(doc.FullName == fullname)
{
ret = true;
break;
}
}
}
catch
{
ret = false;
}
finally
{
if (document != null)
{
Marshal.ReleaseComObject(document);
document = null;
}
if (application != null)
{
Marshal.ReleaseComObject(application);
application = null;
}
}
}
return ret;
}
public bool AttivaDocumento(string fullname) // Attiva il doc specificato (gia` aperto)
{
bool ret = false;
try
{
application = (SolidEdgeFramework.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("SolidEdge.Application");
}
catch
{
application = null;
}
if (application != null) // Se SE e` in funzione, legge i documenti aperti e li mette in una lista
{
try
{
foreach (SolidEdgeFramework.SolidEdgeDocument doc in application.Documents)
{
if (doc.FullName == fullname)
{
doc.Activate();
ret = true;
break;
}
}
}
catch
{
ret = false;
}
finally
{
if (document != null)
{
Marshal.ReleaseComObject(document);
document = null;
}
if (application != null)
{
Marshal.ReleaseComObject(application);
application = null;
}
}
}
return ret;
}
public bool ScriviListaProprieta(bool bTutte) // Scrive lista prop nel doc attivo (in lista dati o tutte)
{
bool ret = false;
try
{
application = (SolidEdgeFramework.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("SolidEdge.Application");
}
catch
{
application = null;
}
if(application != null)
{
SolidEdgeFramework.PropertySets propertySets = null; // Proprieta` del documento aperto (non del file)
SolidEdgeFramework.Properties properties = null;
SolidEdgeFramework.Property property = null;
try
{
document = (SolidEdgeFramework.SolidEdgeDocument)application.ActiveDocument;
propertySets = (SolidEdgeFramework.PropertySets)document.Properties;
properties = propertySets.Item("Custom");
foreach(ProprietaSolidEdge pr in listaProprietaFile) // Percorre la lista proprieta`
{
if(listaProprietaDati.Contains(pr.Nome) || bTutte) // Copia se in lista o se flag
property = properties.Add(pr.Nome, pr.Valore);
}
ret = true;
}
catch // Restituisce false se eccezione
{
ret = false;
}
finally
{
if (property != null)
{
Marshal.ReleaseComObject(property);
property = null;
}
if (properties != null)
{
Marshal.ReleaseComObject(properties);
properties = null;
}
if (propertySets != null)
{
Marshal.ReleaseComObject(propertySets);
propertySets = null;
}
if (document != null)
{
Marshal.ReleaseComObject(document);
document = null;
}
if (application != null)
{
Marshal.ReleaseComObject(application);
application = null;
}
}
}
return ret;
}
public bool LeggiListaNomiDati(string fullname) // Legge da file i nomi delle proprieta 'dati'
{
bool ret = false;
FileInfo fileDati = new FileInfo(fullname); // Rif. al file
if(fileDati.Exists) // Se il file esiste...
{
StreamReader fsr = fileDati.OpenText(); // Apre uno stream...
List<string> lst = new List<string>();
string line;
while( (line = fsr.ReadLine()) != null) // Legge tutte le righe
{
lst.Add(line); // e le aggiunge alla lista
}
fsr.Close(); // Chiude lo stream
if(lst.Count > 0) // Se ha letto almeno una riga...
{
listaProprietaDati = lst; // memorizza la lista al posto di quella vecchia
ret = true; // e imposta il flag
}
}
return ret;
}
public string CreaListaScale() // Riempie una lista con le scale delle viste del draft
{
string messaggio = ""; // Cancella i dati preesistenti
listaScale.Clear();
try {
application = (SolidEdgeFramework.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("SolidEdge.Application");
} catch {
messaggio = "Nessuna istanza attiva";
application = null;
}
try {
document = (SolidEdgeFramework.SolidEdgeDocument)application.ActiveDocument;
if (document.Type == DocumentTypeConstants.igDraftDocument) // Se e` un draft...
{
int count, i; // ... estrae le viste del foglio attivo
double scala;
string tScala;
drawingViews = ((SolidEdgeDraft.DraftDocument)document).ActiveSheet.DrawingViews;
count = drawingViews.Count;
for (i = 0; i < count; i++) // Percorre le viste
{
drawingView = drawingViews.Item(i+1);
scala = drawingView.ScaleFactor;
if(scala >= 1.0)
tScala = scala.ToString()+":1";
else
tScala = "1:"+(1.0/scala).ToString();
listaScale.Add(tScala);
}
}
else {
messaggio = "Non e` un draft";
}
} catch (System.Exception ex) // Messaggio se eccezione
{
messaggio = "Eccezione: " + ex.Message + " + Nessun documento attivo";
}
finally {
if (drawingViews != null) {
foreach (SolidEdgeDraft.DrawingView drawingView in drawingViews) {
Marshal.ReleaseComObject(drawingView);
}
}
if (drawingViews != null) {
Marshal.ReleaseComObject(drawingViews);
drawingViews = null;
}
if (document != null) {
Marshal.ReleaseComObject(document);
document = null;
}
if (application != null) {
Marshal.ReleaseComObject(application);
application = null;
}
}
return messaggio;
}
public List<string> ListaScale // Restituisce la lista scale calcolata
{
get { return listaScale; }
}
public string NomeFoglioBackgroundAttivo() // Restituisce il nome del foglio di background attivo
{
string messaggio = ""; // Cancella i dati preesistenti
try {
application = (SolidEdgeFramework.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("SolidEdge.Application");
} catch {
messaggio = "Nessuna istanza attiva";
application = null;
}
try {
document = (SolidEdgeFramework.SolidEdgeDocument)application.ActiveDocument;
if (document.Type == DocumentTypeConstants.igDraftDocument) // Se e` un draft...
{
messaggio = ((SolidEdgeDraft.DraftDocument)document).ActiveSheet.Background.Name;
}
else {
messaggio = "Non e` un draft";
}
} catch (System.Exception ex) // Messaggio se eccezione
{
messaggio = "Eccezione: " + ex.Message + " + Nessun documento attivo";
}
finally {
if (drawingViews != null) {
foreach (SolidEdgeDraft.DrawingView drawingView in drawingViews) {
Marshal.ReleaseComObject(drawingView);
}
}
if (drawingViews != null) {
Marshal.ReleaseComObject(drawingViews);
drawingViews = null;
}
if (document != null) {
Marshal.ReleaseComObject(document);
document = null;
}
if (application != null) {
Marshal.ReleaseComObject(application);
application = null;
}
}
return messaggio;
}
public bool ScriviSingolaProprieta(string nomeP, string valoreP) // Scrive singola prop text nel doc attivo
{
bool ret = false;
try {
application = (SolidEdgeFramework.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("SolidEdge.Application");
} catch {
application = null;
}
if (application != null) {
SolidEdgeFramework.PropertySets propertySets = null; // Proprieta` del documento aperto
SolidEdgeFramework.Properties properties = null;
SolidEdgeFramework.Property property = null;
try {
document = (SolidEdgeFramework.SolidEdgeDocument)application.ActiveDocument;
propertySets = (SolidEdgeFramework.PropertySets)document.Properties;
properties = propertySets.Item("Custom");
property = properties.Add(nomeP, valoreP);
ret = true;
}
catch // Restituisce false se eccezione
{
ret = false;
}
finally {
if (property != null) {
Marshal.ReleaseComObject(property);
property = null;
}
if (properties != null) {
Marshal.ReleaseComObject(properties);
properties = null;
}
if (propertySets != null) {
Marshal.ReleaseComObject(propertySets);
propertySets = null;
}
if (document != null) {
Marshal.ReleaseComObject(document);
document = null;
}
if (application != null) {
Marshal.ReleaseComObject(application);
application = null;
}
}
}
return ret;
}
public string SaveDocumentoAttivo() // Salva e restituisce il nome completo del doc attivo
{
string nomeDoc = "";
try
{
application = (SolidEdgeFramework.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("SolidEdge.Application");
}
catch
{
application = null;
}
if (application != null) // Se SE e` in funzione, cerca di leggere il tipo di documento attivo
{
try
{
document = (SolidEdgeFramework.SolidEdgeDocument)application.ActiveDocument; // Di tipo Draft
nomeDoc = document.FullName;
document.Save();
document.SaveAs(fullpdfDoc); // Salva il documento come pdf
//document.SaveAs(fulldwgDoc); // Salva solo il foglio attivo come dwg
SolidEdgeDraft.Sheet ActiveSheet; // Memorizza il foglio attivo
ActiveSheet = ((SolidEdgeDraft.DraftDocument)document).ActiveSheet;
// Percorre i fogli e li salva singolarmente in dwg
SolidEdgeDraft.Sections sections = null;
SolidEdgeDraft.Section section = null;
SolidEdgeDraft.SectionSheets sectionSheets = null;
SolidEdgeDraft.Sheet sheet = null;
sections = ((SolidEdgeDraft.DraftDocument)document).Sections; // Le sezioni del documento dft
section = sections.WorkingSection; // Sezione con i fogli di lavoro (non background)
sectionSheets = section.Sheets; // Ottiene la lista dei fogli
for (int j = 1; j <= sectionSheets.Count; j++) // Percorre tutti i fogli
{
sheet = sectionSheets.Item(j);
sheet.Activate();
string dwgSuffix = ""; // Se solo 1 foglio, nessuna estensione
if(sectionSheets.Count> 1)
dwgSuffix = dwgShExt + j.ToString();
document.SaveAs(fulldwgDoc + dwgSuffix + dwgExt); // Salva il foglio attivo come dwg con estensione
}
ActiveSheet.Activate(); // Riattiva il foglio memorizzato
}
catch
{
//nomeDoc = "";
}
finally
{
if (document != null)
{
Marshal.ReleaseComObject(document);
document = null;
}
if (application != null)
{
Marshal.ReleaseComObject(application);
application = null;
}
}
}
return nomeDoc;
}
public bool PreparaNomifile() // Imposta i nomi completi del file attivo...
{ // ...solo se e` una dft
bool done = false;
string simplenameDoc = "";
string pathDoc = "";
string codenameDoc = "";
fullnameDoc = "";
fullpdfDoc = "";
fulldwgDoc = ""; // Azzera i nomi
try
{
application = (SolidEdgeFramework.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("SolidEdge.Application");
}
catch
{
application = null;
}
if (application != null) // Se SE e` in funzione, cerca di leggere il tipo di documento attivo
{
try
{
document = (SolidEdgeFramework.SolidEdgeDocument)application.ActiveDocument;
if(document.Type == DocumentTypeConstants.igDraftDocument)
{
fullnameDoc = document.FullName; // Nome completo
string codice = "";
string[] split = document.Name.Split(new Char [] {'.'});
simplenameDoc = split[0]; // Nome senza estensioni
pathDoc = document.Path + SolidEdgeUtils.pathSeparator; // Path con separatore
CreaListaProprietaDaIstanzaSolidEdge(fullnameDoc); // Riempie lista proprieta` del doc attivo
foreach (ProprietaSolidEdge pr in listaProprietaFile) // Legge le proprieta`
{
if(pr.Nome == listaProprietaDati[0]) // Estrae quelle con il primo nome
{ // del file di configurazione
codice = pr.Valore;
}
}
List<string>[] lst = Codici(codice); // Estrae codici ed estensioni
foreach(string s in lst[0])
{
codenameDoc += s; // Nomi senza estensione
}
if(codenameDoc != simplenameDoc) // Corrispondenza nomefile e codice
nomecodicicorrispondono = false;
else
nomecodicicorrispondono = true;
foreach(string s in lst[1]) // Aggiunge estensione