//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Use: Returns two orthogonal vectors depending on the input normal // //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public static void GetOrthoBase(UnitVector normal, out UnitVector xAxis, out UnitVector yAxis) { xAxis = GetOrthoVector(normal); yAxis = normal.CrossProduct(xAxis); }
public static void addProjectCut(PartDocument oDoc, WorkPlane oWpReference, bool manual = false) { SheetMetalComponentDefinition oCompDef = (SheetMetalComponentDefinition)oDoc.ComponentDefinition; WorkPlane oWpWork = oCompDef.WorkPlanes.AddByPlaneAndOffset(oWpReference, 0); oWpWork.Name = "wpWork"; oWpWork.Visible = false; PlanarSketch oSketch = oCompDef.Sketches.Add(oWpWork); ProjectedCut oProjectCut = oSketch.ProjectedCuts.Add(); if (!manual) { int tmpSegmThk = countThicknessSegment(oProjectCut.SketchEntities); int loop = 0; double offset = 1; while (tmpSegmThk != 2) { // Devo spostare il piano se ci sono cose nel mezzo oWpWork.SetByPlaneAndOffset(oWpReference, offset); tmpSegmThk = countThicknessSegment(oProjectCut.SketchEntities); loop++; offset += offset; if (loop == 20) { throw new Exception("Numero massimo offset piano."); } } oProjectCut.Delete(); oProjectCut = oSketch.ProjectedCuts.Add(); } List <ObjectCollection> dataLine = splittoLinea(oProjectCut.SketchEntities); ObjectCollection linea = lengthPerimetro(); TransientGeometry oTransGeom = iApp.TransientGeometry; UnitVector oNormalVector = oSketch.PlanarEntityGeometry.Normal; UnitVector2d oLineDir = linea[1].Geometry.Direction; UnitVector oLineVector = (UnitVector)oTransGeom.CreateUnitVector(oLineDir.X, oLineDir.Y, 0); UnitVector oOffsetVector = (UnitVector)oLineVector.CrossProduct(oNormalVector); UnitVector oDesiredVector = (UnitVector)oTransGeom.CreateUnitVector(0, 1, 0); bool bNaturalOffsetDir; if (oOffsetVector.IsEqualTo(oDesiredVector)) { bNaturalOffsetDir = true; } else { bNaturalOffsetDir = false; } SketchEntitiesEnumerator oSSketchEntitiesEnum = oSketch.OffsetSketchEntitiesUsingDistance(linea, 0.5, bNaturalOffsetDir, false); oProjectCut.Delete(); styleSketch(oSketch.SketchEntities); int countThicknessSegment(SketchEntitiesEnumerator oSketchEntities) { int result = 0; foreach (SketchEntity e in oSketchEntities) { if (e.Type == ObjectTypeEnum.kSketchLineObject) { SketchLine oSketchLine = (SketchLine)e; double length = Math.Round(oSketchLine.Length * 100) / 100; if (length == Math.Round(oCompDef.Thickness.Value * 100) / 100) { result++; } } } return(result); } List <ObjectCollection> splittoLinea(SketchEntitiesEnumerator oSketchEntities) { List <ObjectCollection> tmp = new List <ObjectCollection>(); tmp.Add(iApp.TransientObjects.CreateObjectCollection()); tmp.Add(iApp.TransientObjects.CreateObjectCollection()); tmp.Add(iApp.TransientObjects.CreateObjectCollection()); int indice = 0; foreach (SketchEntity oSketchEntity in oSketchEntities) { if (oSketchEntity.Type != ObjectTypeEnum.kSketchPointObject) { if (oSketchEntity.Type == ObjectTypeEnum.kSketchLineObject) { SketchLine oSketchLine = (SketchLine)oSketchEntity; if (Math.Round(oSketchLine.Length * 100) / 100 == Math.Round(oCompDef.Thickness.Value * 100) / 100) { if (indice == 0) { indice = 1; } else { indice = 2; } } else { tmp[indice].Add(oSketchEntity); } } if (oSketchEntity.Type == ObjectTypeEnum.kSketchArcObject) { tmp[indice].Add(oSketchEntity); } } } foreach (SketchEntity oSketchEntity in tmp[0]) { tmp[2].Add(oSketchEntity); } List <ObjectCollection> result = new List <ObjectCollection>(); result.Add(tmp[1]); result.Add(tmp[2]); return(result); } void styleSketch(SketchEntitiesEnumerator oSketchEntities) { foreach (SketchEntity oSe in oSketchEntities) { if (oSe.Type == ObjectTypeEnum.kSketchLineObject) { SketchLine se = (SketchLine)oSe; se.OverrideColor = iApp.TransientObjects.CreateColor(0, 0, 255); se.LineType = LineTypeEnum.kDashDottedLineType; } else if (oSe.Type == ObjectTypeEnum.kSketchArcObject) { SketchArc sa = (SketchArc)oSe; sa.OverrideColor = iApp.TransientObjects.CreateColor(0, 0, 255); sa.LineType = LineTypeEnum.kDashDottedLineType; } } } ObjectCollection lengthPerimetro() { double l0 = 0; double l1 = 0; foreach (SketchEntity oSE in dataLine[0]) { if (oSE.Type == ObjectTypeEnum.kSketchLineObject) { SketchLine se = (SketchLine)oSE; l0 = l0 + se.Length; } else if (oSE.Type == ObjectTypeEnum.kSketchArcObject) { SketchArc se = (SketchArc)oSE; l0 = l0 + se.Length; } } foreach (SketchEntity oSE in dataLine[1]) { if (oSE.Type == ObjectTypeEnum.kSketchLineObject) { SketchLine se = (SketchLine)oSE; l1 = l1 + se.Length; } else if (oSE.Type == ObjectTypeEnum.kSketchArcObject) { SketchArc se = (SketchArc)oSE; l1 = l1 + se.Length; } } if (l0 > l1) { return(dataLine[0]); } else { return(dataLine[1]); } } }