using System; using System.Collections.Generic; using System.Diagnostics; using System.Drawing; using System.IO; using System.Reflection; using System.Windows.Forms; using SolidWorks.Interop.sldworks; using SolidWorks.Interop.swconst; namespace Source.csproj { public partial class Form1 : Form { private SldWorks swApp; double EnX = 0; double EnY = 0; double EnZ = 0; public Form1() { InitializeComponent(); this.Text = Assembly.GetEntryAssembly().GetName().Name; } private void Form1Shown(object sender, EventArgs e) { if (swApp == null) { swApp = (SldWorks)Activator.CreateInstance(Type.GetTypeFromProgID("SldWorks.Application")); swApp.Visible = true; } } // Bouton Charger la liste des fichiers à analyser private void Button2Click(object sender, EventArgs e) { try { openFileDialog1.InitialDirectory = Application.StartupPath; if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) { Cursor.Current = System.Windows.Forms.Cursors.AppStarting; int nombre = 0; string liste = openFileDialog1.FileName; dataGridView1.Rows.Clear(); foreach(string line in System.IO.File.ReadAllLines(liste, System.Text.Encoding.UTF8)) { try { string[] parts = line.Split(';'); char[] MyChar = {'"'}; string fichier = parts[0].TrimStart(MyChar).TrimEnd(MyChar); dataGridView1.Rows.Add(fichier, "", "", ""); nombre++; } catch (Exception) { continue; } } Cursor.Current = System.Windows.Forms.Cursors.Default; } } catch (Exception) { MessageBox.Show("Erreur lors de l'import du fichier csv, voulez-vous quitter le programme ?"); } } // Bouton Obtenir les cotes d'encombrements extérieures private void Button1Click(object sender, EventArgs e) { string saveLog = string.Empty; int numLigne = 0; while (numLigne < dataGridView1.Rows.Count) { Cursor.Current = System.Windows.Forms.Cursors.WaitCursor; ModelDoc2 Part = null; int longstatus = 0; int longwarnings = 0; string fichierSW = dataGridView1.Rows[numLigne].Cells[0].Value.ToString(); if (!File.Exists(fichierSW)) { saveLog = dataGridView1.Rows[numLigne].Cells[0].Value.ToString() + ";" + "fichier inexistant"; FichierLog("Resultat.csv", saveLog); numLigne++; continue; } if (fichierSW.ToLowerInvariant().Contains(".sldasm")) { Part = ((ModelDoc2)(swApp.OpenDoc6(fichierSW, (int)swDocumentTypes_e.swDocASSEMBLY, 0, "", ref longstatus, ref longwarnings))); } else if (fichierSW.ToLowerInvariant().Contains(".sldprt")) { Part = ((ModelDoc2)(swApp.OpenDoc6(fichierSW, (int)swDocumentTypes_e.swDocPART, 0, "", ref longstatus, ref longwarnings))); } else { saveLog = dataGridView1.Rows[numLigne].Cells[0].Value.ToString() + ";" + "fichier non conforme"; FichierLog("Resultat.csv", saveLog); numLigne++; continue; } DefEncombrement(); dataGridView1.Rows[numLigne].Cells[1].Value = EnX.ToString(); dataGridView1.Rows[numLigne].Cells[2].Value = EnY.ToString(); dataGridView1.Rows[numLigne].Cells[3].Value = EnZ.ToString(); swApp.CloseDoc(fichierSW); saveLog = dataGridView1.Rows[numLigne].Cells[0].Value.ToString() + ";" + dataGridView1.Rows[numLigne].Cells[1].Value.ToString() + ";" + dataGridView1.Rows[numLigne].Cells[2].Value.ToString() + ";" + dataGridView1.Rows[numLigne].Cells[3].Value.ToString(); FichierLog("Resultat.csv", saveLog); numLigne++; Cursor.Current = System.Windows.Forms.Cursors.Default; } } // Bouton Quitter private void Button3Click(object sender, EventArgs e) { Close(); } private double GetMax(double Val1, double Val2, double Val3) { double functionReturnValue = 0; functionReturnValue = Val1; if (Val2 > functionReturnValue) { functionReturnValue = Val2; } if (Val3 > functionReturnValue) { functionReturnValue = Val3; } return functionReturnValue; } private double GetMin(double Val1, double Val2, double Val3) { double functionReturnValue = 0; functionReturnValue = Val1; if (Val2 < functionReturnValue) { functionReturnValue = Val2; } if (Val3 < functionReturnValue) { functionReturnValue = Val3; } return functionReturnValue; } private void DefEncombrement() { const double MaxDouble = 1.79769313486231E+308; const double MinDouble = -1.79769313486231E+308; ModelDoc2 swModel = default(ModelDoc2); object[] vChild = null; object Box = null; double[] BoxArray = new double[6]; double X_max = 0; double X_min = 0; double Y_max = 0; double Y_min = 0; double Z_max = 0; double Z_min = 0; X_max = MinDouble; X_min = MaxDouble; Y_max = MinDouble; Y_min = MaxDouble; Z_max = MinDouble; Z_min = MaxDouble; swModel = (ModelDoc2)swApp.ActiveDoc; if (swModel == null) { MessageBox.Show("Aucun élément 3D n'est chargé !!!"); return; } if (swModel.GetType() == (int)swDocumentTypes_e.swDocASSEMBLY) { Configuration swConfig = default(Configuration); ConfigurationManager swConfigurationMgr = default(ConfigurationManager); Component2 swRootComp = default(Component2); Component2 swChildComp = default(Component2); swConfigurationMgr = (ConfigurationManager)swModel.ConfigurationManager; swConfig = (Configuration)swConfigurationMgr.ActiveConfiguration; swRootComp = (Component2)swConfig.GetRootComponent3(true); int i = 0; vChild = (object[])swRootComp.GetChildren(); for (i = 0; i <= vChild.GetUpperBound(0); i++) { swChildComp = (Component2)vChild[i]; if (swChildComp.Visible == (int)swComponentVisibilityState_e.swComponentVisible) { Box = (object)swChildComp.GetBox(false, false); BoxArray = (double[])Box; X_max = GetMax(BoxArray[0], BoxArray[3], X_max); X_min = GetMin(BoxArray[0], BoxArray[3], X_min); Y_max = GetMax(BoxArray[1], BoxArray[4], Y_max); Y_min = GetMin(BoxArray[1], BoxArray[4], Y_min); Z_max = GetMax(BoxArray[2], BoxArray[5], Z_max); Z_min = GetMin(BoxArray[2], BoxArray[5], Z_min); } } } else if (swModel.GetType() == (int)swDocumentTypes_e.swDocPART) { PartDoc swPart = (PartDoc)swModel; Box = (object)swPart.GetPartBox(true); BoxArray = (double[])Box; X_max = GetMax(BoxArray[0], BoxArray[3], X_max); X_min = GetMin(BoxArray[0], BoxArray[3], X_min); Y_max = GetMax(BoxArray[1], BoxArray[4], Y_max); Y_min = GetMin(BoxArray[1], BoxArray[4], Y_min); Z_max = GetMax(BoxArray[2], BoxArray[5], Z_max); Z_min = GetMin(BoxArray[2], BoxArray[5], Z_min); } else if (swModel.GetType() == (int)swDocumentTypes_e.swDocDRAWING) { MessageBox.Show("Le fichier n'est pas un fichier 3D valide."); return; } EnX = (X_max * 1000.0) - (X_min * 1000.0); EnY = (Y_max * 1000.0) - (Y_min * 1000.0); EnZ = (Z_max * 1000.0) - (Z_min * 1000.0); } private void FichierLog(string fichierLog, string texte) { try { Cursor.Current = System.Windows.Forms.Cursors.WaitCursor; string FicLog = Application.StartupPath + "\\" + fichierLog; StreamWriter Flux = new StreamWriter(FicLog, true); Flux.Write(texte + "\n"); Flux.Close(); return; } // Si il y a une erreur dans le code ci-dessus catch (Exception) { MessageBox.Show("Erreur lors de la sauvegarde du fichier log.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop); } } } }