public static void DimensionWith(this SketchLine l1, SketchLine l2, double value, string linkName, SldWorks swApp) { ModelDoc2 swDoc = (ModelDoc2)swApp.ActiveDoc; bool bl = false; swDoc.ClearSelection2(true); bl = ((SketchSegment)l1).Select(false); bl = ((SketchSegment)l2).Select(true); Point textPoint = new Point((l1.toLine().Start.X + l2.toLine().Start.X) / 2, (l1.toLine().Start.Y + l2.toLine().Start.Y) / 2, (l1.toLine().Start.Z + l2.toLine().Start.Z) / 2); swDoc.AddDimension2(textPoint.X, textPoint.Y, 0); swDoc.ClearSelection2(true); }
public static double DistanceTo(this SketchLine l1, SketchLine l2) { Point p1 = l1.toLine().Start; Point p2 = l1.toLine().End; Point p3 = l2.toLine().Start; Vector v1 = new Vector(p1, p2); Vector v2 = new Vector(p1, p3); Vector s = new Vector( v1.Y * v2.Z - v1.Z * v2.Y, v1.Z * v2.X - v1.X * v2.Z, v1.X * v2.Y - v1.Y * v2.X); double area = s.Len; double h = area / l1.toLine().Len; return(h); }
public static void DimensionWith(this SketchLine l1, SketchLine l2, string linkName, SldWorks swApp) { ModelDoc2 swDoc = (ModelDoc2)swApp.ActiveDoc; bool bl = false; swApp.SetUserPreferenceToggle((int)swUserPreferenceToggle_e.swInputDimValOnCreate, false); swDoc.ClearSelection2(true); bl = ((SketchSegment)l1).Select(false); bl = ((SketchSegment)l2).Select(true); Point textPoint = new Point((l1.toLine().Start.X + l2.toLine().Start.X) / 2, (l1.toLine().Start.Y + l2.toLine().Start.Y) / 2, (l1.toLine().Start.Z + l2.toLine().Start.Z) / 2); DisplayDimension disDim = swDoc.AddDimension2(textPoint.X, textPoint.Y, 0); disDim.SetLinkedText("" + linkName + ""); swDoc.ClearSelection2(true); swApp.SetUserPreferenceToggle((int)swUserPreferenceToggle_e.swInputDimValOnCreate, true); }
public static bool isParallerTo(this SketchLine l1, SketchLine l2, double tolerance) { Vector source = l1.toLine().Dir; Vector target = l2.toLine().Dir; if (Math.Abs(source.unit.X - target.unit.X) < tolerance && Math.Abs(source.unit.Y - target.unit.Y) < tolerance && Math.Abs(source.unit.Z - target.unit.Z) < tolerance) { return(true); } else if (Math.Abs(source.unit.X + target.unit.X) < tolerance && Math.Abs(source.unit.Y + target.unit.Y) < tolerance && Math.Abs(source.unit.Z + target.unit.Z) < tolerance) { return(true); } else { return(false); } }
private void CreateRunnerCube(SketchLine segment, int index) { ModelDoc2 swDoc = (ModelDoc2)swApp.ActiveDoc; swDoc.ClearSelection2(true); bool boolstatus = swDoc.Extension.SelectByID2("前视基准面", "PLANE", 0, 0, 0, false, 0, null, 0); swDoc.SketchManager.InsertSketch(true); #region 画矩形 Line line = segment.toLine(); Vector dir2 = new Vector(line.Dir.Y, -line.Dir.X, 0); Point point1 = new Point(0, 0, 0); Point point2 = new Point(0, 0, 0); if (IsCoincideWithGate(line.Start)) { point1 = line.Start; point1.X += maniW / 2 * dir2.unit.X - maniInsert * line.Dir.unit.X; point1.Y += maniW / 2 * dir2.unit.Y - maniInsert * line.Dir.unit.Y; point1.Z += maniW / 2 * dir2.unit.Z - maniInsert * line.Dir.unit.Z; point2 = line.End; point2.X -= maniW / 2 * dir2.unit.X; point2.Y -= maniW / 2 * dir2.unit.Y; point2.Z -= maniW / 2 * dir2.unit.Z; } else if (IsCoincideWithGate(line.End)) { point1 = line.End; point1.X += maniW / 2 * dir2.unit.X + maniInsert * line.Dir.unit.X; point1.Y += maniW / 2 * dir2.unit.Y + maniInsert * line.Dir.unit.Y; point1.Z += maniW / 2 * dir2.unit.Z + maniInsert * line.Dir.unit.Z; point2 = line.Start; point2.X -= maniW / 2 * dir2.unit.X; point2.Y -= maniW / 2 * dir2.unit.Y; point2.Z -= maniW / 2 * dir2.unit.Z; } else { point1 = line.End; point1.X += maniW / 2 * dir2.unit.X; point1.Y += maniW / 2 * dir2.unit.Y; point1.Z += maniW / 2 * dir2.unit.Z; point2 = line.Start; point2.X -= maniW / 2 * dir2.unit.X; point2.Y -= maniW / 2 * dir2.unit.Y; point2.Z -= maniW / 2 * dir2.unit.Z; } swDoc.SketchManager.CreateCornerRectangle( point1.X, point1.Y, point1.Z, point2.X, point2.Y, point2.Z); #endregion Sketch thisSketch = swDoc.SketchManager.ActiveSketch; Feature thisFet = (Feature)thisSketch; thisFet.Name = "runnerSketch" + index.ToString(); swDoc.ClearSelection2(true); boolstatus = swDoc.Extension.SelectByID2(thisFet.Name, "SKETCH", 0, 0, 0, false, 0, null, 0); //临时特征 Feature myFeature = swDoc.SingleEndExtrusion(0.01, true, true); myFeature.Name = "runnerCube" + index.ToString(); swDoc.ISelectionManager.EnableContourSelection = false; boolstatus = swDoc.EditRebuild3();//退出草图并重建图形 }