public void Create3DPCB() { if (Directory.Exists("D:\\PDM\\")) { pdm_path = "D:\\PDM\\"; } else if (Directory.Exists("C:\\PDM\\")) { pdm_path = "C:\\PDM\\"; } else { MessageBox.Show("Не найден путь PDM"); return; } //iSwApp.CommandInProgress = true; Board board; string filename; filename = iSwApp.GetOpenFileName("Открыть файл", "", "xml Files (*.xml)|*.xml|", out _, out _, out _); //Board.GetFilename(); if (string.IsNullOrWhiteSpace(filename)) { return; } board = Board.GetfromXML(filename); if (board == null) { MessageBox.Show("XML с неверной структурой", "Ошибка чтения файла"); return; } ModelDoc2 swModel; AssemblyDoc swAssy; ModelView activeModelView; iSwApp.UnloadAddIn(sAddinName); //Новая сборка платы double swSheetWidth = 0, swSheetHeight = 0; string boardName; int Errors = 0, Warnings = 0; swAssy = (AssemblyDoc)iSwApp.NewDocument(pdm_path + "EPDM_LIBRARY\\EPDM_SolidWorks\\EPDM_SWR_Templates\\Модуль_печатной_платы.asmdot", (int)swDwgPaperSizes_e.swDwgPaperA2size, swSheetWidth, swSheetHeight); swModel = (ModelDoc2)swAssy; //Сохранение boardName = filename.Remove(filename.Length - 3) + "SLDASM"; swModel.Extension.SaveAs(boardName, (int)swSaveAsVersion_e.swSaveAsCurrentVersion, (int)swSaveAsOptions_e.swSaveAsOptions_UpdateInactiveViews, null, ref Errors, ref Warnings); //********** //Доска Component2 board_body; PartDoc part; ModelDoc2 swCompModel; Feature swRefPlaneFeat, plane; swAssy.InsertNewVirtualPart(null, out board_body); board_body.Select4(false, null, false); swAssy.EditPart(); swCompModel = (ModelDoc2)board_body.GetModelDoc2(); part = (PartDoc)swCompModel; part.SetMaterialPropertyName2("-00", "гост материалы.sldmat", "Rogers 4003C"); int j = 1; do { swRefPlaneFeat = (Feature)swCompModel.FeatureByPositionReverse(j); j++; }while (swRefPlaneFeat.Name != "Спереди"); plane = (Feature)board_body.GetCorresponding(swRefPlaneFeat); plane.Select2(false, -1); swModel.SketchManager.InsertSketch(false); swModel.SketchManager.AddToDB = true; //Эскизы swModel.SketchManager.DisplayWhenAdded = false; //Контур foreach (object skt in board.sketh) { if (skt.GetType().FullName == "SWAddin.Line") { Line sk = (Line)skt; swModel.SketchManager.CreateLine(sk.x1, sk.y1, 0, sk.x2, sk.y2, 0); } if (skt.GetType().FullName == "SWAddin.Arc") { Arc sk = (Arc)skt; swModel.SketchManager.CreateArc(sk.xc, sk.yc, 0, sk.x1, sk.y1, 0, sk.x2, sk.y2, 0, sk.direction); } } swModel.FeatureManager.FeatureExtrusion3(true, false, false, 0, 0, board.thickness, board.thickness, false, false, false, false, 0, 0, false, false, false, false, true, true, true, 0, 0, false); swModel.ClearSelection2(true); //Вырезы if (board.cutout.Count > 2) { plane.Select2(false, -1); swModel.SketchManager.InsertSketch(false); swModel.SketchManager.AddToDB = true; foreach (object skt in board.cutout) { if (skt.GetType().FullName == "SWAddin.Line") { Line sk = (Line)skt; swModel.SketchManager.CreateLine(sk.x1, sk.y1, 0, sk.x2, sk.y2, 0); } if (skt.GetType().FullName == "SWAddin.Arc") { Arc sk = (Arc)skt; swModel.SketchManager.CreateArc(sk.xc, sk.yc, 0, sk.x1, sk.y1, 0, sk.x2, sk.y2, 0, sk.direction); } } swModel.FeatureManager.FeatureCut4(true, false, true, 1, 0, board.thickness, board.thickness, false, false, false, false, 1.74532925199433E-02, 1.74532925199433E-02, false, false, false, false, false, true, true, true, true, false, 0, 0, false, false); } //Отверстия if (board.circles.Count > 0) { List <Circle> items; items = board.circles.FindAll(hole => hole.radius.CompareTo(0.001) > 0); if (items.Count > 0) { plane.Select2(false, -1); swModel.SketchManager.InsertSketch(false); swModel.SketchManager.AddToDB = true; foreach (Circle c in items) { swModel.SketchManager.CreateCircleByRadius(c.xc, c.yc, 0, c.radius); } swModel.FeatureManager.FeatureCut4(true, false, true, 1, 0, board.thickness, board.thickness, false, false, false, false, 1.74532925199433E-02, 1.74532925199433E-02, false, false, false, false, false, true, true, true, true, false, 0, 0, false, false); } items = board.circles.FindAll(hole => hole.radius.CompareTo(0.001) <= 0); if (items.Count > 0) { plane.Select2(false, -1); swModel.SketchManager.InsertSketch(false); swModel.SketchManager.AddToDB = true; foreach (Circle c in items) { swModel.SketchManager.CreateCircleByRadius(c.xc, c.yc, 0, c.radius); } swModel.FeatureManager.FeatureCut4(true, false, true, 1, 0, board.thickness, board.thickness, false, false, false, false, 1.74532925199433E-02, 1.74532925199433E-02, false, false, false, false, false, true, true, true, true, false, 0, 0, false, false); } } //swModel.FeatureManager.FeatureCut3(true, false, true, 1, 0, board.thickness, board.thickness, false, false, false, false, 1.74532925199433E-02, 1.74532925199433E-02, false, false, false, false, false, true, true, true, true, false, 0, 0, false); swModel.SketchManager.DisplayWhenAdded = true; swModel.SketchManager.AddToDB = false; swAssy.HideComponent(); swAssy.ShowComponent(); swModel.ClearSelection2(true); swAssy.EditAssembly(); string path, sample; switch (board.ver) { case 1: path = pdm_path + "Прочие изделия\\ЭРИ"; break; case 2: path = pdm_path + "Прочие изделия\\Footprint"; break; default: path = pdm_path + "Прочие изделия\\ЭРИ"; break; } List <string> allFoundFiles = new List <string>(Directory.GetFiles(path, "*.SLD*", SearchOption.AllDirectories)); allFoundFiles.AddRange(Directory.GetFiles(pdm_path + "Прочие изделия\\ЭРИ\\T", "*.SLD*", SearchOption.AllDirectories)); List <string> allFind; Dictionary <string, string> empty = new Dictionary <string, string>(); Dictionary <string, string> hollow = new Dictionary <string, string>(); Dictionary <string, string> multiple = new Dictionary <string, string>(); foreach (Component comp in board.components) { //Выбор по какому полю искать 3Д модель //sample = comp.part_Number; sample = comp.title.Replace((char)47, (char)95); sample = sample.Replace((char)92, (char)95); if (board.ver == 2) { sample = comp.footprint.Replace((char)47, (char)95); sample = sample.Replace((char)92, (char)95); if ((sample.EndsWith("N") & !sample.EndsWith("DN")) | (sample.EndsWith("M") & !sample.EndsWith("DM")) | (sample.EndsWith("L") & !sample.EndsWith("DL"))) { sample = sample.Remove(sample.Length - 1); } if (sample.EndsWith("DN") | sample.EndsWith("DM") | sample.EndsWith("DL")) { sample = sample.Remove(sample.Length - 2); } } //Ищем пустые футпринты if (string.IsNullOrEmpty(sample)) { if (!hollow.ContainsKey(comp.title)) { hollow.Add(comp.title, comp.title); } } //Ищем повторы allFind = allFoundFiles.FindAll(item => item.IndexOf(sample, StringComparison.OrdinalIgnoreCase) != -1); if (allFind.Count > 1) { if (!multiple.ContainsKey(comp.title)) { multiple.Add(comp.title, comp.footprint); } } //Регистронезависимый поиск comp.fileName = allFoundFiles.Find(item => item.IndexOf(sample, StringComparison.OrdinalIgnoreCase) != -1); //comp.fileName = allFoundFiles.Find(item => item.Contains(sample)); if (string.IsNullOrWhiteSpace(comp.fileName) & (board.ver != 2)) { sample = comp.part_Number.Replace((char)47, (char)95); sample = sample.Replace((char)92, (char)95); comp.fileName = allFoundFiles.Find(item => item.IndexOf(sample, StringComparison.OrdinalIgnoreCase) != -1); } if (string.IsNullOrWhiteSpace(comp.fileName)) { comp.fileName = pdm_path + "Прочие изделия\\ЭРИ\\Zero.SLDPRT"; if (!empty.ContainsKey(sample)) { empty.Add(sample, sample); } } } double[] transforms, dMatrix; string[] coordSys, names; double alfa, beta, gamma, x, y, z; names = new string[board.components.Count]; coordSys = new string[board.components.Count]; dMatrix = new double[16]; transforms = new double[board.components.Count * 16]; for (int i = 0; i < board.components.Count; i++) { names[i] = board.components[i].fileName; } int n = 0; foreach (Component comp in board.components) { alfa = 0; x = comp.x; y = comp.y; //z = comp.z; if (comp.layer == 1) //Если Top { //z = (comp.z + comp.standOff) standOff не учитывается z = board.thickness; beta = -Math.PI / 2; } else //Иначе Bottom { // z = (comp.z - comp.standOff) standOff не учитывается z = 0; beta = Math.PI / 2; } gamma = -(comp.rotation / 180) * Math.PI; dMatrix[0] = Math.Cos(alfa) * Math.Cos(gamma) - Math.Sin(alfa) * Math.Cos(beta) * Math.Sin(gamma); dMatrix[1] = -Math.Cos(alfa) * Math.Sin(gamma) - Math.Sin(alfa) * Math.Cos(beta) * Math.Cos(gamma); dMatrix[2] = Math.Sin(alfa) * Math.Sin(beta); //1 строка матрицы вращения dMatrix[3] = Math.Sin(alfa) * Math.Cos(gamma) + Math.Cos(alfa) * Math.Cos(beta) * Math.Sin(gamma); dMatrix[4] = -Math.Sin(alfa) * Math.Sin(gamma) + Math.Cos(alfa) * Math.Cos(beta) * Math.Cos(gamma); dMatrix[5] = -Math.Cos(alfa) * Math.Sin(beta); //2 строка матрицы вращения dMatrix[6] = Math.Sin(beta) * Math.Sin(gamma); dMatrix[7] = Math.Sin(beta) * Math.Cos(gamma); dMatrix[8] = Math.Cos(beta); //3 строка матрицы вращения dMatrix[9] = x; dMatrix[10] = y; dMatrix[11] = z; //Координаты dMatrix[12] = 1; //Масштаб dMatrix[13] = 0; dMatrix[14] = 0; dMatrix[15] = 0; //Ничего for (int k = 0; k < dMatrix.Length; k++) { transforms[n * 16 + k] = dMatrix[k]; } n++; } //Вставка swAssy.AddComponents3(names, transforms, coordSys); //Фиксация swModel.Extension.SelectAll(); swAssy.FixComponent(); swModel.ClearSelection2(true); activeModelView = (ModelView)swModel.ActiveView; activeModelView.DisplayMode = (int)swViewDisplayMode_e.swViewDisplayMode_ShadedWithEdges; //**************************** UserProgressBar pb; iSwApp.GetUserProgressBar(out pb); //Заполнение поз. обозначений List <Component2> compsColl = new List <Component2>(); //Коллекция из компонентов сборки платы Feature swFeat; Component2 compTemp; pb.Start(0, board.components.Count, "Поиск"); int itm = 0; swFeat = (Feature)swModel.FirstFeature(); while (swFeat != null) { pb.UpdateProgress(itm); //pb.UpdateTitle(itm); if (swFeat.GetTypeName().Equals("Reference")) //Заполняем коллекцию изделиями { compTemp = (Component2)swFeat.GetSpecificFeature2(); compsColl.Add(compTemp); } swFeat = (Feature)swFeat.GetNextFeature(); itm++; } pb.End(); compsColl[0].Name2 = board.designator + (char)32 + "Плата печатная"; //Пререименовываем деталь if (compsColl.Count - 1 == board.components.Count) //Проверка чтобы не сбились поз. обозначения, если появятся значит все правильно иначе они не нужны { for (int i = 0; i < board.components.Count; i++) { compsColl[i + 1].ComponentReference = board.components[i].physicalDesignator; //Заполняем поз. обозначениями } } string estr = ""; iSwApp.LoadAddIn(sAddinName); if (empty.Count != 0 | hollow.Count != 0 | multiple.Count != 0) { if (empty.Count != 0) { estr = "Отсутствуют 3d модели футпринтов" + System.Environment.NewLine; StreamWriter writer = new StreamWriter(filename.Remove(filename.Length - 4) + "_footprint" + ".txt", false); foreach (KeyValuePair <string, string> str in empty) { estr = estr + str.Key + System.Environment.NewLine; writer.WriteLine(str.Value); } writer.Close(); } if (hollow.Count != 0) { estr = estr + "У этих компонентов пустые строки футпринтов" + System.Environment.NewLine; foreach (KeyValuePair <string, string> str in hollow) { estr = estr + str.Key + System.Environment.NewLine; } } if (multiple.Count != 0) { estr = estr + "Найдено несколько футпринтов этих компонентов" + System.Environment.NewLine; StreamWriter writer_m = new StreamWriter(filename.Remove(filename.Length - 4) + "_дубляжи" + ".txt", false); foreach (KeyValuePair <string, string> str in multiple) { estr = estr + str.Key + "->" + str.Value + System.Environment.NewLine; writer_m.WriteLine(str.Key + "->" + str.Value); } writer_m.Close(); } MessageBox.Show(estr, "Внимание"); //swApp.SendMsgToUser2("Не найдены" + estr, 2, 2); } //iSwApp.CommandInProgress = false; //************** }
public void Create3D() { Board board; string filename; ////SolidWorks //Console.WriteLine("Подключение к SldWorks.Application"); //var progId = "SldWorks.Application.27"; //var progType = System.Type.GetTypeFromProgID(progId); //var swApp = System.Activator.CreateInstance(progType) as ISldWorks; //swApp.Visible = true; //Console.WriteLine("Успешное подключение к версии SldWorks.Application " + swApp.RevisionNumber()); //Console.WriteLine(DateTime.Now.ToString()); //Console.CursorSize = 100; filename = iSwApp.GetOpenFileName("Открыть файл", "", "xml Files (*.xml)|*.xml|", out _, out _, out _); //Board.GetFilename(); if (String.IsNullOrWhiteSpace(filename)) { return; } //System.Environment.Exit(0); board = Board.GetfromXML(filename); ModelDoc2 swModel; AssemblyDoc swAssy; ModelView activeModelView; //Новая сборка платы double swSheetWidth = 0, swSheetHeight = 0; string boardName; int Errors = 0, Warnings = 0; swAssy = (AssemblyDoc)iSwApp.NewDocument("D:\\PDM\\EPDM_LIBRARY\\EPDM_SolidWorks\\EPDM_SWR_Templates\\Модуль_печатной_платы.asmdot", (int)swDwgPaperSizes_e.swDwgPaperA2size, swSheetWidth, swSheetHeight); swModel = (ModelDoc2)swAssy; //Сохранение boardName = filename.Remove(filename.Length - 3) + "SLDASM"; Console.WriteLine(boardName); swModel.Extension.SaveAs(boardName, (int)swSaveAsVersion_e.swSaveAsCurrentVersion, (int)swSaveAsOptions_e.swSaveAsOptions_UpdateInactiveViews, null, ref Errors, ref Warnings); //********** //Доска Component2 board_body; PartDoc part; ModelDoc2 swCompModel; Feature swRefPlaneFeat, plane; swAssy.InsertNewVirtualPart(null, out board_body); board_body.Select4(false, null, false); swAssy.EditPart(); swCompModel = (ModelDoc2)board_body.GetModelDoc2(); part = (PartDoc)swCompModel; part.SetMaterialPropertyName2("-00", "гост материалы.sldmat", "Rogers 4003C"); int j = 1; do { swRefPlaneFeat = (Feature)swCompModel.FeatureByPositionReverse(j); j = j + 1; }while (swRefPlaneFeat.Name != "Спереди"); plane = (Feature)board_body.GetCorresponding(swRefPlaneFeat); plane.Select2(false, -1); swModel.SketchManager.InsertSketch(false); swModel.SketchManager.AddToDB = true; //Эскизы foreach (Object skt in board.sketh) { if (skt.GetType().FullName == "ASM_3D.Line") { Line sk = (Line)skt; swModel.SketchManager.CreateLine(sk.x1, sk.y1, 0, sk.x2, sk.y2, 0); } if (skt.GetType().FullName == "ASM_3D.Arc") { Arc sk = (Arc)skt; swModel.SketchManager.CreateArc(sk.xc, sk.yc, 0, sk.x1, sk.y1, 0, sk.x2, sk.y2, 0, sk.direction); } } swModel.FeatureManager.FeatureExtrusion3(true, false, false, 0, 0, board.thickness, board.thickness, false, false, false, false, 0, 0, false, false, false, false, true, true, true, 0, 0, false); swModel.ClearSelection2(true); plane.Select2(false, -1); swModel.SketchManager.InsertSketch(false); swModel.SketchManager.AddToDB = true; swModel.SketchManager.DisplayWhenAdded = false; foreach (Circle c in board.circles) { swModel.SketchManager.CreateCircleByRadius(c.xc, c.yc, 0, c.radius); } swModel.FeatureManager.FeatureCut3(true, false, true, 1, 0, board.thickness, board.thickness, false, false, false, false, 1.74532925199433E-02, 1.74532925199433E-02, false, false, false, false, false, true, true, true, true, false, 0, 0, false); swAssy.HideComponent(); swAssy.ShowComponent(); swModel.ClearSelection2(true); swAssy.EditAssembly(); List <string> allFoundFiles = new List <string>(System.IO.Directory.GetFiles("D:\\PDM\\Прочие изделия\\ЭРИ", "*.*", SearchOption.AllDirectories)); Dictionary <string, string> empty = new Dictionary <string, string>(); foreach (ASM_3D.Component comp in board.components) { comp.fileName = allFoundFiles.Find(item => item.Contains(comp.part_Number)); if (String.IsNullOrWhiteSpace(comp.fileName)) { comp.fileName = "D:\\PDM\\Прочие изделия\\ЭРИ\\Zero.SLDPRT"; if (!empty.ContainsKey(comp.part_Number)) { empty.Add(comp.part_Number, comp.part_Number); } } } double[] transforms, dMatrix; string[] coordSys, names; double alfa, beta, gamma, x, y, z; names = new string[board.components.Count]; coordSys = new string[board.components.Count]; dMatrix = new double[16]; transforms = new double[board.components.Count * 16]; for (int i = 0; i < board.components.Count; i++) { names[i] = board.components[i].fileName; } int n = 0; foreach (ASM_3D.Component comp in board.components) { alfa = 0; x = comp.x; y = comp.y; if (comp.layer == 1) //Если Top { z = (comp.z + comp.standOff); beta = -Math.PI / 2; } else //Иначе Bottom { z = (comp.z - comp.standOff) / 1000; beta = Math.PI / 2; } gamma = -(comp.rotation / 180) * Math.PI; dMatrix[0] = Math.Cos(alfa) * Math.Cos(gamma) - Math.Sin(alfa) * Math.Cos(beta) * Math.Sin(gamma); dMatrix[1] = -Math.Cos(alfa) * Math.Sin(gamma) - Math.Sin(alfa) * Math.Cos(beta) * Math.Cos(gamma); dMatrix[2] = Math.Sin(alfa) * Math.Sin(beta); //1 строка матрицы вращения dMatrix[3] = Math.Sin(alfa) * Math.Cos(gamma) + Math.Cos(alfa) * Math.Cos(beta) * Math.Sin(gamma); dMatrix[4] = -Math.Sin(alfa) * Math.Sin(gamma) + Math.Cos(alfa) * Math.Cos(beta) * Math.Cos(gamma); dMatrix[5] = -Math.Cos(alfa) * Math.Sin(beta); //2 строка матрицы вращения dMatrix[6] = Math.Sin(beta) * Math.Sin(gamma); dMatrix[7] = Math.Sin(beta) * Math.Cos(gamma); dMatrix[8] = Math.Cos(beta); //3 строка матрицы вращения dMatrix[9] = x; dMatrix[10] = y; dMatrix[11] = z; //Координаты dMatrix[12] = 1; //Масштаб dMatrix[13] = 0; dMatrix[14] = 0; dMatrix[15] = 0; //Ничего for (int k = 0; k < dMatrix.Length; k++) { transforms[n * 16 + k] = dMatrix[k]; } n++; } //Вставка swAssy.AddComponents3(names, transforms, coordSys); //Фиксация swModel.Extension.SelectAll(); swAssy.FixComponent(); swModel.ClearSelection2(true); activeModelView = (ModelView)swModel.ActiveView; activeModelView.DisplayMode = (int)swViewDisplayMode_e.swViewDisplayMode_ShadedWithEdges; //**************************** UserProgressBar pb; iSwApp.GetUserProgressBar(out pb); //Заполнение поз. обозначений List <Component2> compsColl = new List <Component2>(); //Коллекция из компонентов сборки платы Feature swFeat; Component2 compTemp; pb.Start(0, board.components.Count, "Поиск"); int itm = 0; swFeat = (Feature)swModel.FirstFeature(); while (swFeat != null) { pb.UpdateProgress(itm); //pb.UpdateTitle(itm); if (swFeat.GetTypeName().Equals("Reference")) //Заполняем коллекцию изделиями { compTemp = (Component2)swFeat.GetSpecificFeature2(); compsColl.Add(compTemp); } swFeat = (Feature)swFeat.GetNextFeature(); itm++; } pb.End(); compsColl[0].Name2 = "Плата"; //Пререименовываем деталь if (compsColl.Count - 1 == board.components.Count) //Проверка чтобы не сбились поз. обозначения, если появятся значит все правильно иначе они не нужны { for (int i = 0; i < board.components.Count; i++) { compsColl[i + 1].ComponentReference = board.components[i].physicalDesignator; //Заполняем поз. обозначениями } } string estr = ""; if (empty.Count != 0) { foreach (KeyValuePair <string, string> str in empty) { estr = estr + str.Value + System.Environment.NewLine; } MessageBox.Show(estr, "Не найдены"); //swApp.SendMsgToUser2("Не найдены" + estr, 2, 2); } //************** //swApp.ExitApp(); //swApp = null; }