/// <summary> /// Function for editing autocad draw if user want to color marks /// </summary> /// <param name="mark">Current deformation mark</param> /// <param name="trans">Current transaction</param> public static void EditIfColor(DeformationMark mark, Transaction trans) { DBText name = null; DBPoint point = null; try { name = (DBText)trans.GetObject(mark.NameId, OpenMode.ForRead); point = (DBPoint)trans.GetObject(mark.PointId, OpenMode.ForRead); } catch (Exception) { WinForms.MessageBox.Show("ERROR: Не удалось найти на чертеже примитивы для марки " + mark.Name, "Ошибка"); throw; } if (mark.Status == false) { ColorText(name, 1); ColorPoint(point, 1); } else { ColorText(name, 3); ColorPoint(point, 3); } }
/// <summary> /// Creates marks if user chose to color marks /// </summary> /// <param name="mark">Current deformation mark</param> /// <param name="trans">Current transaction</param> /// <param name="modSpace">Current model space</param> static void CreateIfColor(DeformationMark mark, Transaction trans, BlockTableRecord modSpace) { if (mark.Status == true) { CreateMarkPrimitive(trans, modSpace, mark.Name, mark.XCoordinate, mark.YCoordinate, mark.ZCoordinate, Color.FromRgb(0, 255, 0)); } else { CreateMarkPrimitive(trans, modSpace, mark.Name, mark.XCoordinate, mark.YCoordinate, mark.ZCoordinate, Color.FromRgb(255, 0, 0)); } }
/// <summary> /// Function for reading marks from excel file /// </summary> /// <param name="fileName">The name of file to read</param> /// <param name="xlApp">The object of Excel app</param> /// <param name="defMarks">A list for deformation marks</param> /// <param name="bearMarks">A list for bearing marks</param> /// <param name="frstBear">The name of first bear mark</param> /// <param name="scndBear">The name of second bear mark</param> /// <param name="thrdBear">The name of third bear mark</param> public static void ExcelReader(string fileName, Excel.Application xlApp, List <DeformationMark> defMarks, List <Mark> bearMarks, string frstBear, string scndBear, string thrdBear) { Excel.Workbooks xlWorkBooks = null; Excel.Workbook xlWorkBook = null; Excel.Worksheet xlWorkSheet = null; xlWorkBooks = xlApp.Workbooks; try { xlWorkBook = xlWorkBooks.Open(fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); } catch (Exception ex) { xlApp.Quit(); ReleaseObject(xlWorkBooks); ReleaseObject(xlWorkSheet); ReleaseObject(xlWorkBook); ReleaseObject(xlApp); throw new Exception("ERROR: не удалось открыть файл с именем " + fileName); } try { xlWorkSheet = (Excel.Worksheet)xlWorkBook.Sheets[1]; } catch (Exception) { xlWorkBook.Close(true, Type.Missing, Type.Missing); xlApp.Quit(); ReleaseObject(xlWorkBooks); ReleaseObject(xlWorkSheet); ReleaseObject(xlWorkBook); ReleaseObject(xlApp); throw new Exception("ERROR: не удалось открыть страницу файла " + fileName + " для чтения"); } var lastCell = xlWorkSheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell); int lastRow = (int)lastCell.Row; for (int i = 2; i <= (int)lastCell.Row; i++) { if (xlWorkSheet.Cells[i, 1].Text.ToString().Equals(frstBear) || xlWorkSheet.Cells[i, 1].Text.ToString().Equals(scndBear) || xlWorkSheet.Cells[i, 1].Text.ToString().Equals(thrdBear)) { try { Mark newBearMark = new Mark(xlWorkSheet.Cells[i, 1].Text.ToString(), Math.Round(Double.Parse(xlWorkSheet.Cells[i, 2].Text.ToString().Replace(",", ".")), 5), Math.Round(Double.Parse(xlWorkSheet.Cells[i, 3].Text.ToString().Replace(",", ".")), 5), Math.Round(Double.Parse(xlWorkSheet.Cells[i, 4].Text.ToString().Replace(",", ".")), 5)); bearMarks.Add(newBearMark); } catch (Exception e) { xlWorkBook.Close(true, Type.Missing, Type.Missing); xlApp.Quit(); ReleaseObject(xlWorkBooks); ReleaseObject(xlWorkSheet); ReleaseObject(xlWorkBook); ReleaseObject(xlApp); throw new Exception("ERROR: неверный формат данных в файле " + fileName); } } else { try { DeformationMark newDefMark = new DeformationMark(xlWorkSheet.Cells[i, 1].Text.ToString(), Math.Round(Double.Parse(xlWorkSheet.Cells[i, 2].Text.ToString().Replace(",", ".")), 5), Math.Round(Double.Parse(xlWorkSheet.Cells[i, 3].Text.ToString().Replace(",", ".")), 5), Math.Round(Double.Parse(xlWorkSheet.Cells[i, 4].Text.ToString().Replace(",", ".")), 5)); defMarks.Add(newDefMark); } catch (Exception e) { xlWorkBook.Close(true, Type.Missing, Type.Missing); xlApp.Quit(); ReleaseObject(xlWorkBooks); ReleaseObject(xlWorkSheet); ReleaseObject(xlWorkBook); ReleaseObject(xlApp); throw new Exception("ERROR: неверный формат данных в файле " + fileName); } } } bearMarks.Sort(Comparer <Mark> .Create((x, y) => x.Name.CompareTo(y.Name))); xlWorkBook.Close(false, Type.Missing, Type.Missing); xlApp.Quit(); ReleaseObject(xlWorkBooks); ReleaseObject(xlWorkSheet); ReleaseObject(xlWorkBook); ReleaseObject(xlApp); }
/// <summary> /// Common function for getting marks from drawing /// </summary> /// <param name="defMarks">List for deformation marks</param> /// <param name="bearMarks">List for bearing marks</param> /// <param name="doc">Current autocad document</param> /// <param name="ed">Current autocad editor</param> /// <param name="db">Current autocad database</param> /// <param name="frstBear">The name of first bear marks</param> /// <param name="scndBear">The name of second bear marks</param> /// <param name="thrdBear">The name of second bear marks</param> public void GetCoordinates(List <DeformationMark> defMarks, List <Mark> bearMarks, Document doc, Editor ed, Database db, string frstBear, string scndBear, string thrdBear) { List <ObjectId> pointsIds = CreateEntityFilter(ed, "POINT"); List <ObjectId> namesIds = CreateEntityFilter(ed, "TEXT"); List <ObjectId> freePoints = new List <ObjectId>(pointsIds); using (Transaction tr = db.TransactionManager.StartTransaction()) { //Here we will get all names from the drawing foreach (ObjectId nameId in namesIds) { DBText name = (DBText)tr.GetObject(nameId, OpenMode.ForRead); if (name.TextString.Trim().Equals(frstBear) || name.TextString.Trim().Equals(scndBear) || name.TextString.Trim().Equals(thrdBear)) { Mark newBearMark = new Mark { Name = name.TextString.Trim(), NameId = nameId }; bearMarks.Add(newBearMark); } else { DeformationMark newDefMark = new DeformationMark { Name = name.TextString.Trim(), NameId = nameId }; defMarks.Add(newDefMark); } } foreach (ObjectId pointId in pointsIds) { DBPoint point = (DBPoint)tr.GetObject(pointId, OpenMode.ForRead); Matrix3d mat = ed.CurrentUserCoordinateSystem.Inverse(); Point3d wcsPoint = point.Position; Point3d ucsPoint = wcsPoint.TransformBy(mat); foreach (DeformationMark mark in defMarks) { DBText name = (DBText)tr.GetObject(mark.NameId, OpenMode.ForRead); if (ContainMarkAndName(ucsPoint, name, ed)) { mark.XCoordinate = Math.Round(ucsPoint.X, 5); mark.YCoordinate = Math.Round(ucsPoint.Y, 5); mark.ZCoordinate = Math.Round(ucsPoint.Z, 5); mark.PointId = pointId; freePoints.Remove(pointId); break; } } } foreach (ObjectId pointId in freePoints) { DBPoint point = (DBPoint)tr.GetObject(pointId, OpenMode.ForRead); Matrix3d mat = ed.CurrentUserCoordinateSystem.Inverse(); Point3d wcsPoint = point.Position; Point3d ucsPoint = wcsPoint.TransformBy(mat); foreach (Mark mark in bearMarks) { DBText name = (DBText)tr.GetObject(mark.NameId, OpenMode.ForRead); if (ContainMarkAndName(ucsPoint, name, ed)) { mark.XCoordinate = Math.Round(ucsPoint.X, 5); mark.YCoordinate = Math.Round(ucsPoint.Y, 5); mark.ZCoordinate = Math.Round(ucsPoint.Z, 5); mark.PointId = pointId; } } } bearMarks.Sort(Comparer <Mark> .Create((x, y) => x.Name.CompareTo(y.Name))); tr.Commit(); } }