public float GetBestSnap(out Magnet magnet) { float minSnap = SnapController.SnapViewDistance; magnet = null; foreach (float key in snapSqDistances.Keys) { if (key < minSnap) { minSnap = key; } } if (minSnap < SnapController.SnapViewDistance) { foreach (Magnet m in snapSqDistances[minSnap]) { if (magnet == null) { magnet = m; } else if (((PointMagnet)m).Type == PointMagnetType.EndPoint) { magnet = m; } if (((PointMagnet)m).Type == PointMagnetType.EndPoint) { return(minSnap); } } } return(minSnap); }
public float GetBestSnap(out Magnet magnet) { float minSnap = SnapController.SnapViewDistance; magnet = null; foreach (float key in snapSqDistances.Keys) { if (key < minSnap) { minSnap = key; } } if (minSnap < SnapController.SnapViewDistance) { magnet = snapSqDistances[minSnap]; } // Override CurrentLine if it's an axis and another LineMagnet // attached to a LineElement si found if (magnet != null && currentLine != null && (currentLine.Type == LineMagnetType.FollowXAxis || currentLine.Type == LineMagnetType.FollowYAxis || currentLine.Type == LineMagnetType.FollowZAxis) && ((LineMagnet)magnet).Type != LineMagnetType.FollowXAxis && ((LineMagnet)magnet).Type != LineMagnetType.FollowYAxis && ((LineMagnet)magnet).Type != LineMagnetType.FollowZAxis) { CurrentLine = (LineMagnet)magnet; } return(minSnap); }
public SnapController() { paintMagnets = new List <Magnet>(); snapMagnet = null; points = new PointMagnetsCollection(); lines = new LineMagnetsCollection(); area = new AreaMagnet(PointMagnet.ZeroMagnet.Position, CommonAxes.GlobalAxes[2]); painter = new SnapPainter(); }
public SnapController() { paintMagnets = new List<Magnet>(); snapMagnet = null; points = new PointMagnetsCollection(); lines = new LineMagnetsCollection(); area = new AreaMagnet(PointMagnet.ZeroMagnet.Position, CommonAxes.GlobalAxes[2]); painter = new SnapPainter(); }
/// <summary> /// This method paints all the gadgets added by the Snaps /// (i.e. /// </summary> public void Paint(Device device, GraphicView activeView, System.Windows.Forms.MouseEventArgs e) { for (int i = 0; i < paintMagnets.Count; i++) { Magnet m = paintMagnets[i]; if (m != null) { if (m is PointMagnet) { byte alpha = 255; if (m.LastSnapFitness >= EffectiveSnapDistance) { alpha = (byte)((1f - (m.LastSnapFitness - EffectiveSnapDistance) / (SnapViewDistance - EffectiveSnapDistance)) * 128f + 64f); } switch (((PointMagnet)m).Type) { case PointMagnetType.Perpendicular: if (alpha == 255 && m.RelatedMagnets.Count > 0 && m.RelatedMagnets[0] is PointMagnet) { painter.PaintLineSegment(device, activeView, m.Position, m.RelatedMagnets[0].Position, LineMagnetType.FollowHelper); } break; case PointMagnetType.SimplePoint: if (alpha == 255 && m.RelatedMagnets.Count > 0 && m.RelatedMagnets[0] is PointMagnet) { painter.PaintPointSymbol(device, activeView, m.RelatedMagnets[0].Position, PointMagnetType.SimplePoint, 192); } break; } painter.PaintPointSymbol(device, activeView, m.Position, ((PointMagnet)m).Type, alpha); } else if (m is LineMagnet) { LineMagnetType lmt = ((LineMagnet)m).Type; if (lmt == LineMagnetType.FollowProjection && lines.CurrentLine != null) { lmt = LineMagnetType.FollowHelper; } painter.PaintLineSegment(device, activeView, m.Position, m.SnapPositionInt, lmt); } } } if (lines.CurrentLine != null) { painter.PaintLineSegment(device, activeView, lines.CurrentLine.Position, lines.CurrentLine.SnapPositionInt, lines.CurrentLine.Type); } }
/// <summary> /// This method returns a Magnet whose SnapPoint property has the point obtained either typed or clicked. /// </summary> /// <param name="prompt">The instruction text that appears at the SmallPanel.</param> /// <returns>The Magnet whose SnapPoint was found.</returns> public Snap.Magnet GetPoint(string prompt) { controller.MainFrm.SmallPanel.Start(controller.ModelCommand.Title, prompt, 10, "1,2,3 " + Culture.Get("or") + " @1,2,3"); wait(WaitingFor.Point, true); Snap.Magnet m = waitingObj as Snap.Magnet; if (m == null) { return(null); } lastPoint = m.SnapPosition; SnapPrimaryPoint = new Snap.PointMagnet(m.SnapPositionInt, Snap.PointMagnetType.SimplePoint); return(m); }
/// <summary> /// Method to perform calculations based on the user interaction by /// moving the mouse /// </summary> /// <param name="sender">The object calling this method</param> /// <param name="e">The Mouse event args</param> /// <returns>A boolean indicating whether a repaint is necessary</returns> public bool MouseMove(GraphicView activeView, System.Windows.Forms.MouseEventArgs e) { bool ret = true; paintMagnets.Clear(); pickItems(e); recalcPrimaryDependant(activeView); // Check snap to acquired magnets points.Snap(activeView, e); lines.Snap(activeView, e); // Get max snap Magnet (the one closer to the mouse position) Magnet bestMagnet; float snap, bestSnap, bestPointSnap = points.GetBestSnap(out bestMagnet); if (bestMagnet == null) { bestSnap = lines.GetBestSnap(out bestMagnet); } else { bestSnap = bestPointSnap; } // Update CurrentLine's positions if mouse moved over any of it's joints (if LineMagnet has a Line) if (lines.CurrentLine != null && bestMagnet != null && bestMagnet is PointMagnet && lines.CurrentLine.Line != null && ((PointMagnet)bestMagnet).Joint != null) { Joint newJoint = ((PointMagnet)bestMagnet).Joint; if (newJoint == lines.CurrentLine.Line.I || newJoint == lines.CurrentLine.Line.J) { lines.CurrentLine.Position = newJoint.Position; } } #region Intersection // Check intersections between LineMagnets. If any, create corresponding Magnet if (lines.CurrentLine != null && bestMagnet is LineMagnet && bestMagnet != lines.CurrentLine) { PointMagnet intersectionMagnet = createIntersection(lines.CurrentLine, bestMagnet as LineMagnet, activeView, e); if (intersectionMagnet != null) { snap = intersectionMagnet.Snap(activeView, e.Location) + SnapEpsilon; if (snap < SnapViewDistance) { // Paint helper line paintMagnets.Add(bestMagnet); // Paint intersection paintMagnets.Add(intersectionMagnet); } if (snap < bestPointSnap && snap < EffectiveSnapDistance) { bestSnap = snap; bestMagnet = intersectionMagnet; } } } #endregion #region Midpoint // Create midpoint magnet (if mouse is close to a Line) foreach (Magnet m in lines) { PointMagnet midMagnet = createMidPoint(m as LineMagnet, activeView, e); if (midMagnet != null) { snap = midMagnet.Snap(activeView, e.Location) + SnapEpsilon; if (snap < bestPointSnap && snap < EffectiveSnapDistance) { bestSnap = snap; bestMagnet = midMagnet; } } } #endregion #region Perpendicular point // Create perpendicular magnets (if mouse is close to a line) PointMagnet perpMagnet = createPerpendicularMagnet(bestMagnet as LineMagnet, activeView, e); if (perpMagnet != null) { snap = perpMagnet.Snap(activeView, e.Location) + SnapEpsilon; if (snap < (bestPointSnap - SnapEpsilon) && snap < EffectiveSnapDistance) { bestSnap = snap; bestMagnet = perpMagnet; } } #endregion #region Interesting Distance // If following a LineMagnet, then set next interesting distance as a PointMagnet PointMagnet distMagnet = createInterestingDistance(bestMagnet as LineMagnet, activeView, e); if (distMagnet != null) { snap = distMagnet.Snap(activeView, e.Location) + SnapEpsilon; if (snap < bestPointSnap) { bestSnap = snap; bestMagnet = distMagnet; } } #endregion // Choose magnets to paint if ((bestMagnet is LineMagnet) && (lines.CurrentLine == null)) { lines.CurrentLine = (LineMagnet)bestMagnet; } else if ((bestMagnet != null) && !bestMagnet.Equals(lines.CurrentLine)) { paintMagnets.Add(bestMagnet); } // Set the Magnet to snap to if the user clicks the mouse snapMagnet = null; if (bestMagnet != null) { if (bestMagnet is PointMagnet && bestMagnet.LastSnapFitness < EffectiveSnapDistance) { snapMagnet = bestMagnet; } else if (lines.CurrentLine != null) { snapMagnet = lines.CurrentLine; } else if (bestMagnet is LineMagnet) { snapMagnet = bestMagnet; } } else { area.Snap(activeView, e.Location); snapMagnet = (Magnet)area.Clone(); } // TODO: Probably change the IsActive property to some enum, so as to activate snapping to // specific objects only (i.e. Joint snapping when CommandServices.GetJoint() is working) return(ret); }
/// <summary> /// Method to perform calculations based on the user interaction by /// moving the mouse /// </summary> /// <param name="sender">The object calling this method</param> /// <param name="e">The Mouse event args</param> /// <returns>A boolean indicating whether a repaint is necessary</returns> public bool MouseMove(GraphicView activeView, System.Windows.Forms.MouseEventArgs e) { bool ret = true; paintMagnets.Clear(); pickItems(e); recalcPrimaryDependant(activeView); // Check snap to acquired magnets points.Snap(activeView, e); lines.Snap(activeView, e); // Get max snap Magnet (the one closer to the mouse position) Magnet bestMagnet; float snap, bestSnap, bestPointSnap = points.GetBestSnap(out bestMagnet); if (bestMagnet == null) bestSnap = lines.GetBestSnap(out bestMagnet); else bestSnap = bestPointSnap; // Update CurrentLine's positions if mouse moved over any of it's joints (if LineMagnet has a Line) if (lines.CurrentLine != null && bestMagnet != null && bestMagnet is PointMagnet && lines.CurrentLine.Line != null && ((PointMagnet)bestMagnet).Joint != null) { Joint newJoint = ((PointMagnet)bestMagnet).Joint; if (newJoint == lines.CurrentLine.Line.I || newJoint == lines.CurrentLine.Line.J) lines.CurrentLine.Position = newJoint.Position; } #region Intersection // Check intersections between LineMagnets. If any, create corresponding Magnet if (lines.CurrentLine != null && bestMagnet is LineMagnet && bestMagnet != lines.CurrentLine) { PointMagnet intersectionMagnet = createIntersection(lines.CurrentLine, bestMagnet as LineMagnet, activeView, e); if (intersectionMagnet != null) { snap = intersectionMagnet.Snap(activeView, e.Location) + SnapEpsilon; if (snap < SnapViewDistance) { // Paint helper line paintMagnets.Add(bestMagnet); // Paint intersection paintMagnets.Add(intersectionMagnet); } if (snap < bestPointSnap && snap < EffectiveSnapDistance) { bestSnap = snap; bestMagnet = intersectionMagnet; } } } #endregion #region Midpoint // Create midpoint magnet (if mouse is close to a Line) foreach (Magnet m in lines) { PointMagnet midMagnet = createMidPoint(m as LineMagnet, activeView, e); if (midMagnet != null) { snap = midMagnet.Snap(activeView, e.Location) + SnapEpsilon; if (snap < bestPointSnap && snap < EffectiveSnapDistance) { bestSnap = snap; bestMagnet = midMagnet; } } } #endregion #region Perpendicular point // Create perpendicular magnets (if mouse is close to a line) PointMagnet perpMagnet = createPerpendicularMagnet(bestMagnet as LineMagnet, activeView, e); if (perpMagnet != null) { snap = perpMagnet.Snap(activeView, e.Location) + SnapEpsilon; if (snap < (bestPointSnap - SnapEpsilon) && snap < EffectiveSnapDistance) { bestSnap = snap; bestMagnet = perpMagnet; } } #endregion #region Interesting Distance // If following a LineMagnet, then set next interesting distance as a PointMagnet PointMagnet distMagnet = createInterestingDistance(bestMagnet as LineMagnet, activeView, e); if (distMagnet != null) { snap = distMagnet.Snap(activeView, e.Location) + SnapEpsilon; if (snap < bestPointSnap) { bestSnap = snap; bestMagnet = distMagnet; } } #endregion // Choose magnets to paint if ((bestMagnet is LineMagnet) && (lines.CurrentLine == null)) lines.CurrentLine = (LineMagnet)bestMagnet; else if ((bestMagnet != null) && !bestMagnet.Equals(lines.CurrentLine)) paintMagnets.Add(bestMagnet); // Set the Magnet to snap to if the user clicks the mouse snapMagnet = null; if (bestMagnet != null) { if (bestMagnet is PointMagnet && bestMagnet.LastSnapFitness < EffectiveSnapDistance) snapMagnet = bestMagnet; else if (lines.CurrentLine != null) snapMagnet = lines.CurrentLine; else if (bestMagnet is LineMagnet) snapMagnet = bestMagnet; } else { area.Snap(activeView, e.Location); snapMagnet = (Magnet)area.Clone(); } // TODO: Probably change the IsActive property to some enum, so as to activate snapping to // specific objects only (i.e. Joint snapping when CommandServices.GetJoint() is working) return ret; }
protected void copyToMagnet(Magnet dstMagnet) { dstMagnet.position = position; dstMagnet.lastSnapFitness = lastSnapFitness; dstMagnet.relatedMagnets = (relatedMagnets != null) ? new List<Magnet>(relatedMagnets) : null; }
protected void copyToMagnet(Magnet dstMagnet) { dstMagnet.position = position; dstMagnet.lastSnapFitness = lastSnapFitness; dstMagnet.relatedMagnets = (relatedMagnets != null) ? new List <Magnet>(relatedMagnets) : null; }