Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Strugling with AddCoincident method

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
M_F_T_
240 Views, 4 Replies

Strugling with AddCoincident method

Hi all, basically what Im trying to do is to create a rectangle by using "AddAsTwoPointCenteredRectangle" command and make that rectangles center Coincident to center point of the sketch.

 

Here is my code 

Sub Main()
    ' Define the inputs for the dimensions of the first rectangle
    Dim length1 As Double
    Dim width1 As Double

    ' Prompt the user to input the dimensions of the first rectangle
    length1 = CDbl(InputBox("Enter the length of the first rectangle", "Rectangle Dimensions", "10"))
    width1 = CDbl(InputBox("Enter the width of the first rectangle", "Rectangle Dimensions", "5"))

    Dim oPartCompDef As PartComponentDefinition
    oPartCompDef = ThisApplication.ActiveDocument.ComponentDefinition

    Dim oSketch As PlanarSketch
    oSketch = oPartCompDef.Sketches.Add(oPartCompDef.WorkPlanes.Item(3))

    Dim halfLength1 As Double
    Dim halfWidth1 As Double
    halfLength1 = length1 / 2
    halfWidth1 = width1 / 2

    Dim oRectangleLines As Object
    With ThisApplication.TransientGeometry
        oRectangleLines = oSketch.SketchLines.AddAsTwoPointCenteredRectangle( _
                                        .CreatePoint2d(0, 0), _
                                        .CreatePoint2d(halfLength1, halfWidth1))
    End With

    Dim oLine1 As SketchLine
    Dim oLine2 As SketchLine
    Dim oLine3 As SketchLine
    Dim oLine4 As SketchLine

    oLine1 = oRectangleLines(1)
    oLine2 = oRectangleLines(2)
    oLine3 = oRectangleLines(3)
    oLine4 = oRectangleLines(4)

    Dim oOriginPoint As SketchPoint
    oOriginPoint = oSketch.OriginPoint

    Dim oMidPoint As Point2d
    oMidPoint = oLine1.Geometry.MidPoint

    Dim oMidSketchPoint As SketchPoint
    oMidSketchPoint = oSketch.SketchPoints.Add(oMidPoint)
     
'This is the place where Im getting the error messages Call oSketch.GeometricConstraints.AddCoincident(oMidSketchPoint, oOriginPoint) End Sub

 Im gettin this error mesage;

System.ArgumentException: Parametre hatalı. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Inventor.GeometricConstraints.AddCoincident(SketchEntity EntityOne, SketchEntity EntityTwo)
at ThisRule.Main() in rule: Rectangle, in document Core(V-01).ipt:line 47
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

 

Can someone give me an idea ?

M_F_T__0-1721307537686.png

 




M.Fatih TÜRKER
4 REPLIES 4
Message 2 of 5
davidt162003
in reply to: M_F_T_

I think the problem might be that the sketch origin isn't set by default. heres the code i use to do the same. 

    Friend Sub CoincidentCnsrt(SketchPnt1 As SketchPoint, SketchPnt2 As SketchPoint, Sketch As PlanarSketch)
        Try
            Sketch.GeometricConstraints.AddCoincident(SketchPnt1, SketchPnt2)
        Catch ex As Exception

        End Try
    End Sub
    Friend Sub CoincidentAtOrgin(SketchPnt As SketchPoint, Sketch As PlanarSketch, CompDef As PartComponentDefinition)
        Dim OrginSkPnt = CheckOrginSkPnt(CompDef, Sketch)
        CoincidentCnsrt(SketchPnt, OrginSkPnt, Sketch)
    End Sub

 this is just adding in the constraint and calls a sub to make sure the model origin has been projected. 

 Friend Function CheckOrginSkPnt(CompDef As PartComponentDefinition, Sketch As PlanarSketch) As SketchPoint
     Dim OrginSkPnt As SketchPoint = Nothing
     For Each pSkPnt As SketchPoint In Sketch.SketchPoints
         If pSkPnt.ReferencedEntity IsNot Nothing Then
             OrginSkPnt = pSkPnt
         End If
     Next
     If OrginSkPnt Is Nothing Then
         OrginSkPnt = Sketch.AddByProjectingEntity(CompDef.WorkPoints(1))
     End If
     Return OrginSkPnt
 End Function

Just added your call the function above using the sketch object and the sketch point you want to constrain to the origin

HII
Message 3 of 5
WCrihfield
in reply to: M_F_T_

Hi @M_F_T_.  First of all, you will need to change this block of code:

 

    Dim oRectangleLines As Object
    With ThisApplication.TransientGeometry
        oRectangleLines = oSketch.SketchLines.AddAsTwoPointCenteredRectangle( _
                                        .CreatePoint2d(0, 0), _
                                        .CreatePoint2d(halfLength1, halfWidth1))
    End With

 

...to be like this:

 

Dim oRectangleLines As SketchEntitiesEnumerator
	oRectangleLines = oSketch.SketchLines.AddAsTwoPointCenteredRectangle(oOriginPoint, _
	oTG.CreatePoint2d(halfLength1, halfWidth1))

 

Then, you may need to either choose a different type of constraint, or different entities to constrain.  You would not want to constrain the center point of the new rectangle to one of the midpoints of one of the rectangle line mid-points.  That would be impossible.  You may instead be able to use a horizontal or vertical type constraint between the two points, instead of a coincident one.  Or use the interior constructions lines, which cross each other at the center to constrain one of their mid points to the center point, using a midpoint constraint, instead of a coincident constraint.  Below is an example of your modified code that works for me.

 

Sub Main
	' Define the inputs for the dimensions of the first rectangle
	Dim length1 As Double
	Dim width1 As Double

	' Prompt the user to input the dimensions of the first rectangle
	length1 = CDbl(InputBox("Enter the length of the first rectangle", "Rectangle Dimensions", "10"))
	width1 = CDbl(InputBox("Enter the width of the first rectangle", "Rectangle Dimensions", "5"))

	Dim oPartCompDef As PartComponentDefinition = ThisApplication.ActiveDocument.ComponentDefinition
	Dim oOriginWP As WorkPoint = oPartCompDef.WorkPoints.Item(1)
	Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
	
	Dim oSketch As PlanarSketch
	oSketch = oPartCompDef.Sketches.Add(oPartCompDef.WorkPlanes.Item(3))
	
	oSketch.OriginPoint = oOriginWP
	Dim oOriginPoint As SketchPoint = oSketch.AddByProjectingEntity(oOriginWP)
	
	Dim halfLength1 As Double
	Dim halfWidth1 As Double
	halfLength1 = length1 / 2
	halfWidth1 = width1 / 2
	
	Dim oRectangleLines As SketchEntitiesEnumerator
	oRectangleLines = oSketch.SketchLines.AddAsTwoPointCenteredRectangle(oOriginPoint, _
	oTG.CreatePoint2d(halfLength1, halfWidth1))

	Dim oLine1 As SketchLine = oRectangleLines(1)
	Dim oLine2 As SketchLine = oRectangleLines(2)
	Dim oLine3 As SketchLine = oRectangleLines(3)
	Dim oLine4 As SketchLine = oRectangleLines(4)
	'the two construction lines crossing the center
	Dim oLine5 As SketchLine = oRectangleLines(5)
	Dim oLine6 As SketchLine = oRectangleLines(6)

	Dim oMidPoint As Point2d = oLine5.Geometry.MidPoint

	Dim oMidSketchPoint As SketchPoint = oSketch.SketchPoints.Add(oMidPoint, False)
	oSketch.GeometricConstraints.AddMidpoint(oMidSketchPoint, oLine5)
End Sub

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 4 of 5
M_F_T_
in reply to: M_F_T_

Thank you all for your kind help. What I understand so far is that so we should project center point in either way in order to do that proccess. Am I right ?

 

Also another quick question is ;

Can you advise a little tip for how to do this shape in most effective way ? even though no answer, still thank you 🙂

M_F_T__0-1721388321324.png

 

M.Fatih TÜRKER
Message 5 of 5
davidt162003
in reply to: M_F_T_

yes you will need to project the centre point.
as for the most effcient way to create that shape it depends on a few things:
Do you need the lines inside it?
What your going to do with it after? IE are you going to revolve, extrude or ect.
and creating it through the API or through the UI?
Personally id draw a quarter of the shape using lines then either do a mirror within the sketch or mirror the feature its used for.
HII

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report