protected override bool OnClickStart(ScreenPoint cursorPos, Line cursorRay) { IDocObject selection = null; IDocObject preselection = InteractionContext.Preselection; var desFace = preselection as DesignFace; if (desFace != null) { WriteBlock.ExecuteTask("Create Sphere Set", () => selection = SphereSet.Create(desFace, count, strength, radius, color).Subject); } else { SphereSet sphereSet = SphereSet.GetWrapper(preselection as CustomObject); if (sphereSet != null) { selection = sphereSet.Subject; CountSlider.Value = sphereSet.Count; StrengthSlider.Value = sphereSet.Strength; ColorComboBox.Value = sphereSet.Color; RadiusSlider.Value = sphereSet.Radius; } } Window.ActiveContext.Selection = new[] { selection }; return(false); }
void WiggleThread() { WaitHandle[] waitHandles = new WaitHandle[] { exitThreadEvent }; int step = 0; DateTime startTime = DateTime.Now; int sleep = 200; // 5 frames per second while (EventWaitHandle.WaitAny(waitHandles, 0, false) != 0) { System.Windows.Forms.Application.OpenForms[0].BeginInvoke(new MethodInvoker(delegate { // wrap to make thread-safe WriteBlock.ExecuteTask("Iterate Wiggle", delegate { double time = (DateTime.Now - startTime).TotalSeconds; wiggleGroup.SetDimensionValue(wiggleInitialValue + wiggleAmplitude * Math.Sin(time * 2 * Math.PI / wiggleFrequency)); //foreach (IDocObject docObject in Window.ActiveWindow.Selection) { // ITransformable geometry = docObject as ITransformable; // //if (geometry == null) // // geometry = docObject.GetParent<IDesignBody>(); // if (geometry == null) // return; // Matrix newTrans = Window.ActiveWindow.Projection.Inverse * StartViewTrans; // geometry.Transform(newTrans * LastTrans.Inverse); // LastTrans = newTrans; //} }); })); Thread.Sleep(sleep); step++; } }
void FreezeThread() { WaitHandle[] waitHandles = new WaitHandle[] { exitThreadEvent }; int step = 0; int sleep = 100; // 10 frames per second while (EventWaitHandle.WaitAny(waitHandles, 0, false) != 0) { System.Windows.Forms.Application.OpenForms[0].BeginInvoke(new MethodInvoker(delegate { // wrap to make thread-safe WriteBlock.ExecuteTask("Iterate Freeze", delegate { foreach (IDocObject docObject in Window.ActiveWindow.ActiveContext.Selection) { ITransformable geometry = docObject as ITransformable; //if (geometry == null) // geometry = docObject.GetParent<IDesignBody>(); if (geometry == null) { return; } Matrix newTrans = Window.ActiveWindow.Projection.Inverse * StartViewTrans; geometry.Transform(newTrans * LastTrans.Inverse); LastTrans = newTrans; } }); })); Thread.Sleep(sleep); step++; } }
void AnimateThread() { WaitHandle[] waitHandles = new WaitHandle[] { exitThreadEvent }; int step = 0; startTime = DateTime.Now; int sleep = 100; // 10 frames per second while (EventWaitHandle.WaitAny(waitHandles, 0, false) != 0) { System.Windows.Forms.Application.OpenForms[0].BeginInvoke(new MethodInvoker(delegate { // wrap to make thread-safe WriteBlock.ExecuteTask("Iterate Animation", delegate { double time = (DateTime.Now - startTime).TotalSeconds; foreach (Component component in componentAnimations.Keys) { component.Placement = Matrix.Identity; component.Transform(componentAnimations[component].GetTransform(time)); } }); })); Thread.Sleep(sleep); step++; } }
protected override bool OnClickStart(ScreenPoint cursorPos, Line cursorRay) { var iDesignFace = InteractionContext.Preselection as IDesignFace; if (iDesignFace == null) { return(false); } SurfaceEvaluation eval = iDesignFace.Shape.GetSingleRayIntersection(cursorRay); if (eval == null) { return(false); } double defaultHeight = 0.02; WriteBlock.ExecuteTask("Create Oscillator", () => { OscillatorPart.Create(HandleTypeEnum.Shaft, iDesignFace, eval.Param, defaultHeight, 0); OscillatorPart.Create(HandleTypeEnum.Base, iDesignFace, eval.Param, defaultHeight, 0); OscillatorPart.Create(HandleTypeEnum.Start, iDesignFace, eval.Param, defaultHeight, 0); OscillatorPart.Create(HandleTypeEnum.End, iDesignFace, eval.Param, defaultHeight, 0); }); return(false); }
private void StartThreadImages() { isThreadRunning = true; Part mainPart = Window.ActiveWindow.Scene as Part; thread = new System.Threading.Thread((System.Threading.ThreadStart) delegate { while (isThreadRunning) { buttonCapsule.UpdateProperties(); // Window.ActiveWindow.SetProjection(Matrix.CreateRotation(Line.Create(Point.Origin, Direction.DirY), 2 * Math.PI / 300) * Window.ActiveWindow.Projection, false, false); WriteBlock.ExecuteTask("Save JPEG", delegate { lawson.CreateCircularTabsOnly(mainPart); Window.ActiveWindow.Export(WindowExportFormat.Jpeg, System.IO.Path.GetTempPath() + lawson.Iteration.ToString("TumbleFrame{0000.}.jpg")); foreach (Component component in mainPart.Components) { component.Delete(); } } ); lawson.Iterate(); // Rendering = Graphic.Create(null, null, LawsonRelaxGraphics.GetGraphic(lawson)); StatusText = string.Format("Iteration {0} Error {1} Max {2}", lawson.Iteration, lawson.CumulativeError, lawson.MaxError); } }); thread.Start(); }
void TumbleThread() { WaitHandle[] waitHandles = new WaitHandle[] { exitThreadEvent }; int step = 0; int savingImagesStartStep = step; int sleep = 1000 / 40; // 40 frames per second Matrix transformStep; Part part = Window.ActiveWindow.Scene as Part; while (EventWaitHandle.WaitAny(waitHandles, 0, false) != 0) { // System.Windows.Forms.Application.OpenForms[0].Invoke(new MethodInvoker(delegate { // wrap to make thread-safe System.Windows.Forms.Application.OpenForms[0].BeginInvoke(new MethodInvoker( delegate { // wrap to make thread-safe double timeScale = speed * sleep / (1000 * 60); transformStep = Iterate(timeScale * 2 * Math.PI, step); if (!isCenteredOnSelection) { Window.ActiveWindow.SetProjection(transformStep * Window.ActiveWindow.Projection, false, false); } else { Matrix translation = Matrix.CreateTranslation(Window.ActiveWindow.Projection * Center.Vector); Window.ActiveWindow.SetProjection(translation * transformStep * translation.Inverse * Window.ActiveWindow.Projection, false, false); } if (IsChangingColors) { // Command.GetCommand("AEColorsVaryHue").Execute(); WriteBlock.ExecuteTask("Set Colors", delegate { foreach (IDesignBody iDesBody in part.GetDescendants <IDesignBody>()) { HSBColor HsbColor = new HSBColor(iDesBody.Master.GetVisibleColor()); HsbColor.H += (float)360 * (float)timeScale; iDesBody.Master.SetColor(null, HsbColor.Color); } } ); } if (IsSavingImages) { if (step - savingImagesStartStep < 1 / timeScale) { WriteBlock.ExecuteTask("Save PNG", delegate { Window.ActiveWindow.Export(WindowExportFormat.Png, System.IO.Path.GetTempPath() + step.ToString("TumbleFrame{0000.}.png")); } ); } } step++; } )); Thread.Sleep(sleep); } }
void colorCommand_TextChanged(object sender, CommandTextChangedEventArgs e) { color = ColorComboBox.Value; SphereSet sphereSet = SelectedSphereSet; if (sphereSet != null) { WriteBlock.ExecuteTask("Adjust color", () => sphereSet.Color = color); } }
void strengthSliderCommand_TextChanged(object sender, CommandTextChangedEventArgs e) { strength = StrengthSlider.Value; SphereSet sphereSet = SelectedSphereSet; if (sphereSet != null) { WriteBlock.ExecuteTask("Adjust radius", () => sphereSet.Strength = strength); } }
void radiusSliderCommand_TextChanged(object sender, CommandTextChangedEventArgs e) { radius = RadiusSlider.Value; SphereSet sphereSet = SelectedSphereSet; if (sphereSet != null) { WriteBlock.ExecuteTask("Adjust radius", () => sphereSet.Radius = radius); } }
public void Reset() { WriteBlock.ExecuteTask("Reset Animation", delegate { startTime = DateTime.Now; foreach (Component component in componentAnimations.Keys) { component.Placement = componentAnimations[component].InitialPosition; } }); }
// static Create method follows the API convention and parent should be first argument public static FaceToolPathObject Create(IDesignFace desFace, FaceToolPath toolPath, Color color) { Debug.Assert(desFace == null || desFace.Master.Shape == toolPath.Face); FaceToolPathObject toolPathObj = null; WriteBlock.ExecuteTask("New toolpath", () => toolPathObj = new FaceToolPathObject(desFace, toolPath, color)); toolPathObj.Initialize(); // IList<CutterLocation> cutterLocations; // toolPath.TryGetCutterLocations(out cutterLocations); // toolPathObj.cutterLocations = cutterLocations; return(toolPathObj); }
private static T DoInWriteBlock <T>(Func <T> func) { if (WriteBlock.IsActive) { return(func()); } else { T result = default(T); WriteBlock.ExecuteTask("API busywork", () => { result = func(); }); return(result); } }
public override int Advance(int frame) { Debug.Assert(frame >= 1); SphereSet sphereSet = DistributeSpheresTool.SelectedSphereSet; if (sphereSet == null) { return(frame); } WriteBlock.ExecuteTask("Animate Spheres", () => CalculateFrame(sphereSet)); return(frame + 2); }
protected override void OnEnable(bool enable) { if (enable) { Window.SelectionChanged += Window_SelectionChanged; HandleSelectionChanged(); } else { Window.SelectionChanged -= Window_SelectionChanged; if (prototypeObj != null && !prototypeObj.IsDeleted) { WriteBlock.ExecuteTask("Delete path", () => prototypeObj.Delete()); } } }
protected override bool OnClickStart(ScreenPoint cursorPos, Line cursorRay) { DesignFace designFace = InteractionContext.Preselection as DesignFace; if (designFace == null) { return(false); } CurveSegment innerCurve, outerCurveA, outerCurveB; CreateThreadCurves(designFace.Shape, pitch, angle, positionOffset, out innerCurve, out outerCurveA, out outerCurveB); WriteBlock.ExecuteTask(Resources.ThreadStructureText, () => { DesignBody.Create(designFace.Parent.Parent, Resources.ThreadStructureText, CreateThreadBody(designFace.Shape, pitch, angle, positionOffset)); }); return(false); }
public override void SaveFile(string path) { mainPart = Window.ActiveWindow.Scene as Part; if (mainPart == null) { return; } path = mainPart.Document.Path; WriteBlock.ExecuteTask("Adjust visibility", new Task(delegate { mainPart.Export(PartExportFormat.Step, path, true, null); })); path = Path.Combine(Path.GetDirectoryName(path), Path.GetFileNameWithoutExtension(path)); foreach (IComponent component in mainPart.Components) { RecurseComponents(path, component); } }
protected override bool OnClickStart(ScreenPoint cursorPos, Line cursorRay) { DesignEdge designEdge = InteractionContext.Preselection as DesignEdge; if (designEdge == null) { return(false); } Circle edgeCircle = (Circle)designEdge.Shape.Geometry; Frame frame = edgeCircle.Frame; Face face = designEdge.Faces.Where(f => f.Shape.Geometry is Plane).First().Shape; Plane plane = (Plane)face.Geometry; if (frame.DirZ == plane.Frame.DirZ ^ !face.IsReversed) { frame = Frame.Create(frame.Origin, -frame.DirZ); } double angle = apiGroove.Angle; double depth = apiGroove.Depth; var points = new[] { Point.Create(apiGroove.InnerDiameter / 2, 0, 0), Point.Create(apiGroove.InnerDiameter / 2 + Math.Sin(angle) * depth, 0, -Math.Cos(angle) * depth), Point.Create(apiGroove.OuterDiameter / 2 - Math.Sin(angle) * depth, 0, -Math.Cos(angle) * depth), Point.Create(apiGroove.OuterDiameter / 2, 0, 0) }; var profile = points.AsPolygon(); var path = new[] { CurveSegment.Create(Circle.Create(Frame.World, 1)) }; WriteBlock.ExecuteTask("Create Profile", () => { var body = Body.SweepProfile(Plane.PlaneZX, profile, path); body.Transform(Matrix.CreateMapping(frame)); var cutter = DesignBody.Create(designEdge.Parent.Parent, "temp", body); designEdge.Parent.Shape.Subtract(new[] { cutter.Shape }); }); return(false); }
private void RecurseComponents(string namePath, IComponent component) { if (!Directory.Exists(namePath)) { Directory.CreateDirectory(namePath); } string newNamePath = Path.Combine(namePath, component.Master.Template.Name); string fileName = newNamePath + ".stp"; if (File.Exists(fileName)) { return; } WriteBlock.ExecuteTask("Adjust visibility", new Task(delegate { foreach (IDesignBody body in mainPart.GetDescendants <IDesignBody>()) { body.SetVisibility(null, false); } foreach (IDesignBody body in component.Content.GetDescendants <IDesignBody>()) { body.SetVisibility(null, null); } mainPart.Export(PartExportFormat.Step, fileName, true, null); foreach (IDesignBody body in mainPart.GetDescendants <IDesignBody>()) { body.SetVisibility(null, null); } })); foreach (IComponent c in component.Content.Components) { RecurseComponents(newNamePath + "-", c); } }
public BezierPatches(String path) { StreamReader file = File.OpenText(path); string line; Match match; // Patches indices int patchCount; line = file.ReadLine(); match = Regex.Match(line, @"(\d+)"); if (!match.Success) { Warn(line); return; } if (!int.TryParse(match.Groups[1].Value, out patchCount)) { Warn(line); return; } patchIndices = new int[patchCount][, ]; for (int i = 0; i < patchCount; i++) { if (file.EndOfStream) { Warn("Premature end of file"); return; } line = file.ReadLine(); match = Regex.Match(line, @"(\d+),(\d+),(\d+),(\d+),(\d+),(\d+),(\d+),(\d+),(\d+),(\d+),(\d+),(\d+),(\d+),(\d+),(\d+),(\d+)"); if (!match.Success) { Warn(line); return; } int i00, i01, i02, i03; int i10, i11, i12, i13; int i20, i21, i22, i23; int i30, i31, i32, i33; if ( int.TryParse(match.Groups[1].Value, out i00) && int.TryParse(match.Groups[2].Value, out i01) && int.TryParse(match.Groups[3].Value, out i02) && int.TryParse(match.Groups[4].Value, out i03) && int.TryParse(match.Groups[5].Value, out i10) && int.TryParse(match.Groups[6].Value, out i11) && int.TryParse(match.Groups[7].Value, out i12) && int.TryParse(match.Groups[8].Value, out i13) && int.TryParse(match.Groups[9].Value, out i20) && int.TryParse(match.Groups[10].Value, out i21) && int.TryParse(match.Groups[11].Value, out i22) && int.TryParse(match.Groups[12].Value, out i23) && int.TryParse(match.Groups[13].Value, out i30) && int.TryParse(match.Groups[14].Value, out i31) && int.TryParse(match.Groups[15].Value, out i32) && int.TryParse(match.Groups[16].Value, out i33) ) { patchIndices[i] = new int[, ] { { i00, i01, i02, i03 }, { i10, i11, i12, i13 }, { i20, i21, i22, i23 }, { i30, i31, i32, i33 } }; continue; } Warn(line); return; } // vertices int vertexCount; line = file.ReadLine(); match = Regex.Match(line, @"(\d+)"); if (!match.Success) { Warn(line); return; } if (!int.TryParse(match.Groups[1].Value, out vertexCount)) { Warn(line); return; } vertices = new Point[vertexCount]; for (int i = 0; i < vertexCount; i++) { if (file.EndOfStream) { Warn("Premature end of file"); return; } line = file.ReadLine(); match = Regex.Match(line, @"([-\d\.]+),([-\d\.]+),([-\d\.]+)"); if (!match.Success) { Warn(line); return; } double x, y, z; if (double.TryParse(match.Groups[1].Value, out x) && double.TryParse(match.Groups[2].Value, out y) && double.TryParse(match.Groups[3].Value, out z)) { vertices[i] = Point.Create(x, y, z); continue; } Warn(line); return; } WriteBlock.ExecuteTask("Create Patches", CreateGeometry); }
static void Spackler(object sender, EventArgs e) { WriteBlock.ExecuteTask("Spackle", delegate { SpackleSome(); }); }
private new void Commit() { WriteBlock.ExecuteTask("Commit", () => base.Commit()); }
public override void WriteXml(Matrix documentTrans) { Debug.Assert(iTrimmedCurves != null); Debug.Assert(iTrimmedCurves.Count > 0); string pathData = MoveTo(documentTrans * iTrimmedCurves[0].StartPoint); foreach (ITrimmedCurve curve in iTrimmedCurves) { ITrimmedCurve iTrimmedCurve = curve; Line line = iTrimmedCurve.Geometry as Line; if (line != null) { pathData += LineTo(documentTrans * iTrimmedCurve.EndPoint); continue; } if (iTrimmedCurve.Geometry is Circle || iTrimmedCurve.Geometry is Ellipse) { ITrimmedCurve iTrimmedEllipse = iTrimmedCurve.ProjectToPlane(Plane.PlaneXY); Ellipse ellipse = iTrimmedEllipse.Geometry as Ellipse; if (ellipse == null) { ellipse = ((Circle)iTrimmedEllipse.Geometry).AsEllipse(); } Debug.Assert(ellipse != null); Debug.Assert(iTrimmedEllipse.Bounds.Span > 0); // interval should always be positive for SC ellipses Debug.Assert(!iTrimmedEllipse.IsReversed); double majorRadius = documentTrans.Scale * Math.Max(ellipse.MajorRadius, ellipse.MinorRadius); double minorRadius = documentTrans.Scale * Math.Min(ellipse.MajorRadius, ellipse.MinorRadius); double x = Vector.Dot(ellipse.Frame.DirX.UnitVector, Direction.DirX.UnitVector); double y = Vector.Dot(ellipse.Frame.DirX.UnitVector, Direction.DirY.UnitVector); pathData += ArcTo( majorRadius, minorRadius, Math.Atan2(y, x) * 180 / Math.PI, iTrimmedEllipse.Bounds.Span > Math.PI, Direction.Equals(Direction.DirZ, ellipse.Frame.DirZ), documentTrans * iTrimmedEllipse.EndPoint ); continue; } NurbsCurve nurbsCurve = iTrimmedCurve.Geometry as NurbsCurve; if (nurbsCurve == null) { ProceduralCurve proceduralCurve = iTrimmedCurve.Geometry as ProceduralCurve; if (proceduralCurve != null) { nurbsCurve = proceduralCurve.AsSpline(iTrimmedCurve.Bounds); } } if (nurbsCurve != null) { Debug.Assert(nurbsCurve.Data.Degree == 3); SelectFragmentResult result = null; CurveSegment fullNurbsCurve = CurveSegment.Create(nurbsCurve); List <ITrimmedCurve> pointCurves = nurbsCurve.Data.Knots .Where(k => iTrimmedCurve.Bounds.Contains(k.Parameter)) .Select(k => ((ITrimmedCurve)(nurbsCurve.Evaluate(k.Parameter).Point.AsCurveSegment()))) .ToList <ITrimmedCurve>(); pointCurves.Insert(0, nurbsCurve.Evaluate(iTrimmedCurve.Bounds.Start).Point.AsCurveSegment()); pointCurves.Add(nurbsCurve.Evaluate(iTrimmedCurve.Bounds.End).Point.AsCurveSegment()); for (int i = 0; i < pointCurves.Count - 1; i++) { //try { result = fullNurbsCurve.SelectFragment(( nurbsCurve.ProjectPoint(pointCurves[i].StartPoint).Param + nurbsCurve.ProjectPoint(pointCurves[i + 1].StartPoint).Param ) / 2, pointCurves); //} //catch { // Debug.Assert(false, "Attempted to trim curve out of bounds."); // WriteBlock.ExecuteTask("SVG Exception Curves", () => DesignCurve.Create(Window.ActiveWindow.Scene as Part, iTrimmedCurve)); // break; //} if (result != null) { NurbsCurve fragment = result.SelectedFragment.Geometry as NurbsCurve; WriteBlock.ExecuteTask("curve", delegate { fragment.Print(); }); pathData += CubicBézierTo(documentTrans * fragment.ControlPoints[1].Position, documentTrans * fragment.ControlPoints[2].Position, documentTrans * fragment.ControlPoints[3].Position); } } continue; } PointCurve pointCurve = iTrimmedCurve.Geometry as PointCurve; if (pointCurve != null) { continue; } // Handle Polygons, which are not in the API foreach (Point point in iTrimmedCurve.Geometry.GetPolyline(iTrimmedCurve.Bounds).Skip(1)) { pathData += LineTo(documentTrans * point); } continue; // throw new NotSupportedException("Unhandled iTrimmedCurve"); } if (isClosed) { pathData += ClosePath(); } xmlWriter.WriteStartElement("path"); xmlWriter.WriteAttributeString("d", pathData); StyleAttributes(strokeColor, fillColor, strokeWidth); xmlWriter.WriteEndElement(); }
public override void SaveFile(string path) { WriteBlock.ExecuteTask("Copy and slice", () => SaveFileWork(path)); }
public CcpFile(String path) { Elements = new List <CppElement>(); StreamReader file = File.OpenText(path); string line; var profiles = new List <List <Point> >(); Match match; while (!file.EndOfStream) { line = file.ReadLine(); // Comment lines if (Regex.Match(line, @"^/\*").Success) { continue; } match = Regex.Match(line, @"HEX_NUMCOMP\s*=\s*(\d+)"); if (match.Success) { int components; if (int.TryParse(match.Groups[1].Value, out components)) { Components = components; for (int i = 0; i < components; i++) { Elements.Add(new CppElement()); } continue; } Warn(line); break; } match = Regex.Match(line, @"NUM_RAYS\s*=\s*(\d+)"); if (match.Success) { int rays; if (int.TryParse(match.Groups[1].Value, out rays)) { Rays = rays; continue; } Warn(line); break; } match = Regex.Match(line, @"ENVELOPE\s*=\s*(\w+)"); if (match.Success) { string value = match.Groups[1].Value; if (value == "YES") { IsEnvelope = true; continue; } if (value == "NO") { IsEnvelope = false; continue; } Warn(line); break; } match = Regex.Match(line, @"F(\d\d)TYP\s*=\s*""(.+)"""); if (match.Success) { int index; if (int.TryParse(match.Groups[1].Value, out index)) { Elements[index - 1].Type = match.Groups[2].Value; Elements[index - 1].Type = Elements[index - 1].Type.Replace(' ', '"'); continue; } Warn(line); break; } match = Regex.Match(line, @"F(\d\d)DEN\s*=\s*""(\d+)"""); if (match.Success) { int index; double value; if ( int.TryParse(match.Groups[1].Value, out index) && double.TryParse(match.Groups[2].Value, out value) ) { Elements[index - 1].Density = value / 1000; // TBD guessing that density units are also scaled to mm continue; } Warn(line); break; } match = Regex.Match(line, @"([FB])(\d\d)(\w+)\s*=\s*([-\d\.]+)"); if (match.Success) { string side = match.Groups[1].Value; int index; string key = match.Groups[3].Value; double value; if ((side == "F" || side == "B") && int.TryParse(match.Groups[2].Value, out index) && double.TryParse(match.Groups[4].Value, out value) ) { CppSideData sideData = side == "F" ? Elements[index - 1].Front : Elements[index - 1].Back; double units; if (key == "A" || key == "B" || key == "M") { units = -Math.PI / 180; // angular units are specified in degrees (TBD sign only tested for A rotations, may be different for others) } else { units = 0.001; // linear units are specified in mm } sideData.ElementValues[key] = value * units; continue; } Warn(line); break; } } WriteBlock.ExecuteTask("Import CCP data", CreateGeometry); }