Add coincident constraint between line midpoint and an arbitary point on the line

Add coincident constraint between line midpoint and an arbitary point on the line

A.Acheson
Mentor Mentor
652 Views
3 Replies
Message 1 of 4

Add coincident constraint between line midpoint and an arbitary point on the line

A.Acheson
Mentor
Mentor

 

Hi all, having trouble with this task adding a coincident constraint between two projected Curves  (their midpoint) and new line added.  I have had luck with add midpoint and the new line StartSketchPoint and EndSketchPoint but I was unable to just position the line in the center points but allowing it to travel freely along those two points.

 

 

Dim curve1 As DrawingCurve = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kDrawingCurveSegmentFilter, "Select a drawing Curve").Parent

Dim curve2 As DrawingCurve = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kDrawingCurveSegmentFilter, "Select a drawing Curve").Parent

Dim drawView As DrawingView = ThisDoc.Document.ActiveSheet.DrawingViews.Item(1)

Dim sketch As DrawingSketch = drawView.Sketches.Add

sketch.Edit()

Dim line1 as SketchLine = sketch.AddByProjectingEntity(curve1)

Dim line2 as SketchLine = sketch.AddByProjectingEntity(curve2)

Dim line3 As SketchLine = sketch.SketchLines. _
    AddByTwoPoints(tg.CreatePoint2d(0, 10),tg.CreatePoint2d(5, 10))

sketch.GeometricConstraints.AddCoincident(line1.Midpoint,line3)
sketch.GeometricConstraints.AddCoincident(line2.MidPoint,line3)

sketch.ExitEdit()

 

 

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Accepted solutions (2)
653 Views
3 Replies
Replies (3)
Message 2 of 4

WCrihfield
Mentor
Mentor

Hi Alan.  Just throwing a couple ideas out there that could be tested...

- Have you tried reversing the order of the two objects within each constraint (midpoint second, instead of first)?  Just thinking about that because it often makes some difference which one you choose first and second when creating some constraints manually.  Kind of line one is thought of as staying in place, while the other should be moved, or something like that.

- I wander if it has to do with the first two lines being projected (and fully grounded).  Could test test the exact same process manually and see if it shows a warning dialog.  Could also try creating the initial two lines normally (not projected, and not fully grounded, but maybe constrained), then see if it will allow the two constraints by code.  If that works, then that would isolate if it is because of the projected / grounded geometry in the mix.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 4

WCrihfield
Mentor
Mentor
Accepted solution

I see now that the SketchLine object does not have a property called MidPoint, but the SketchLine.Geometry property, which represents a LineSegment2d object, does have a MidPoint property, but that is a Point2d type object, not a SketchPoint type object, and I doubt we can constrain to a Point2d, because it is transient.  The help documentation for this AddCoincident method says that one of the inputs must be a SketchPoint.  So, you may have to constrain a SketchPoint to both of those two initial lines, in the right place first, then constrain the third line through those two added SketchPoints.

Edit:  Yep.  I just tested it myself.  Manually created a sketch in a drawing view, then projected two straight view lines into the sketch, then manually drew a line which generally crosses both, but is not constrained at all, then created and ran this following rule code (while the sketch was still in edit mode), and it worked as planned.

Dim oDSketch As DrawingSketch = ThisApplication.ActiveEditObject
Dim oLines As SketchLines = oDSketch.SketchLines
Dim oSPs As SketchPoints = oDSketch.SketchPoints
Dim oGCs As GeometricConstraints = oDSketch.GeometricConstraints
Dim oLine1 As SketchLine = oLines.Item(1)
Dim oLine2 As SketchLine = oLines.Item(2)
Dim oLine3 As SketchLine = oLines.Item(3)
Dim oSP1 As SketchPoint = oSPs.Add(oLine1.Geometry.MidPoint, False)
Dim oSP2 As SketchPoint = oSPs.Add(oLine2.Geometry.MidPoint, False)
Dim oC1 As CoincidentConstraint = oGCs.AddCoincident(oSP1, oLine3)
Dim oC2 As CoincidentConstraint = oGCs.AddCoincident(oSP2, oLine3)

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 4 of 4

A.Acheson
Mentor
Mentor
Accepted solution

Hi Wesley thanks for the pointers, the sketch point was what I was missing. I had actually tried that approach but didn't really understand it and then abandoned it. With your approach the sketch points were attached to the new line. So all I needed to do was to attach the new sketch point to the midpoint of the projected line first then attach the new line to the constrained sketch point.

Here is an article discussing sketchpoint interaction with sketchlines. 

From this article, this paragraph is key.

"The thing to learn from this is that all sketch geometry is always associated to  sketch points.  Sketch geometry doesn’t exist on its own.  The position of sketch geometry is controlled by the position of the points.  It’s like the geometry is fastened to the points and if the points move the geometry attached to them also moves. "

 

AAcheson_1-1698438737633.png

 

Working Sample

 

 

Dim curve1 As DrawingCurve = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kDrawingCurveSegmentFilter, "Select a drawing Curve").Parent
Dim curve2 As DrawingCurve = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kDrawingCurveSegmentFilter, "Select a drawing Curve").Parent

Dim drawView As DrawingView = curve1.Parent

Dim sketch As DrawingSketch = drawView.Sketches.Add

sketch.Edit()

Dim line1 As SketchLine = sketch.AddByProjectingEntity(curve1)
Dim line2 As SketchLine = sketch.AddByProjectingEntity(curve2)

Dim tg As TransientGeometry = ThisApplication.TransientGeometry

Dim line3 As SketchLine = sketch.SketchLines. _
    					AddByTwoPoints(tg.CreatePoint2d(0, 10),tg.CreatePoint2d(5, 10))

Dim skPts As SketchPoints = sketch.SketchPoints
Dim geometricCons As GeometricConstraints = sketch.GeometricConstraints

Dim lineSeg1 As LineSegment2d = line1.Geometry
Dim lineSeg2 As LineSegment2d = line2.Geometry

Dim skPt1 As SketchPoint = skPts.Add(lineSeg1.MidPoint, False)
Dim skPt2 As SketchPoint = skPts.Add(lineSeg2.MidPoint, False)

Dim midpoint1 As MidpointConstraint = geometricCons.AddMidpoint(skPt1, line1)
Dim midpoint2 As MidpointConstraint = geometricCons.AddMidpoint(skPt2,line2)

Dim coincident1 As CoincidentConstraint = geometricCons.AddCoincident(line3, skPt1)
Dim coincident2 As CoincidentConstraint = geometricCons.AddCoincident(line3,skPt2)

sketch.ExitEdit()

 

 

 

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan