to constraint a Rectangle from centerpoint .

to constraint a Rectangle from centerpoint .

Anonymous
Not applicable
947 Views
3 Replies
Message 1 of 4

to constraint a Rectangle from centerpoint .

Anonymous
Not applicable

Hello All , 

   I need a help regarding constraining a sketch from CenterPoint  in inventor by C# or VB. I am making a rectangle from (0 , 0) . but it is not fully constraint ..Please Let me know if there be any solution .

Thank You...

0 Likes
Accepted solutions (1)
948 Views
3 Replies
Replies (3)
Message 2 of 4

HideoYamada
Advisor
Advisor

Hello,

 

I made sample code. Create new part document and then run this VBA macro.

Sub test()
    Dim partDoc As PartDocument
    Set partDoc = ThisApplication.ActiveDocument
    
    Dim compDef As PartComponentDefinition
    Set compDef = partDoc.ComponentDefinition
    
    Dim sketch As PlanarSketch
    Set sketch = compDef.Sketches.Add(compDef.WorkPlanes(1))
    sketch.Edit
    
    Dim centerPoint As SketchPoint
    Set centerPoint = sketch.AddByProjectingEntity(compDef.WorkPoints(1))
    
    Dim cornerPoint As Point2d
    Set cornerPoint = ThisApplication.TransientGeometry.CreatePoint2d(5, 5)
        
    Dim entities As SketchEntitiesEnumerator
    Set entities = sketch.SketchLines.AddAsTwoPointCenteredRectangle(centerPoint, cornerPoint)
    
    CreateLengthConstraint entities(1)
    CreateLengthConstraint entities(2)
    
    partDoc.Update
    sketch.ExitEdit
End Sub

Sub CreateLengthConstraint(line As SketchLine)
    Dim point1 As SketchPoint
    Dim point2 As SketchPoint
    Dim textPoint As Point2d
    
    Set point1 = line.Constraints(1).EntityTwo
    Set point2 = line.Constraints(2).EntityTwo
    Set textPoint = ThisApplication.TransientGeometry.CreatePoint2d((point1.Geometry.X + point2.Geometry.X) / 2, (point1.Geometry.Y + point2.Geometry.Y) / 2)
        
    Call line.Parent.DimensionConstraints.AddTwoPointDistance(point1, point2, kAlignedDim, textPoint)
End Sub

There may be more better(simple) way...

 

Does this code help you?

 

=====

Freeradical

 Hideo Yamada

 

=====
Freeradical
 Hideo Yamada
https://www.freeradical.jp
Message 3 of 4

Anonymous
Not applicable

Actually i am working on c#..so i am getting a problm while defining centerpoint...in vb it is working

 

0 Likes
Message 4 of 4

HideoYamada
Advisor
Advisor
Accepted solution

Ok, I translated to C#.

private void DrawRectangle(PartDocument partDoc)
{
    PartComponentDefinition compDef = partDoc.ComponentDefinition;
    PlanarSketch sketch = compDef.Sketches.Add(compDef.WorkPlanes[1]);

    sketch.Edit();

    SketchPoint centerPoint = (SketchPoint)sketch.AddByProjectingEntity(compDef.WorkPoints[1]);
    Point2d cornerPoint = m_inventorApplication.TransientGeometry.CreatePoint2d(5, 5);
    SketchEntitiesEnumerator entities = sketch.SketchLines.AddAsTwoPointCenteredRectangle(centerPoint, cornerPoint);

    CreateLengthConstraint((SketchLine)entities[1]);
    CreateLengthConstraint((SketchLine)entities[2]);

    partDoc.Update();
    sketch.ExitEdit();
}

private void CreateLengthConstraint(SketchLine line)
{
    SketchPoint point1 = ((line.Constraints[1]) as CoincidentConstraint)?.EntityTwo as SketchPoint;
    SketchPoint point2 = ((line.Constraints[2]) as CoincidentConstraint)?.EntityTwo as SketchPoint;
    if (point1 == null || point2 == null) return;

    Point2d textPoint = m_inventorApplication.TransientGeometry.CreatePoint2d((point1.Geometry.X + point2.Geometry.X) / 2, (point1.Geometry.Y + point2.Geometry.Y) / 2);
    line.Parent.DimensionConstraints.AddTwoPointDistance(point1, point2, DimensionOrientationEnum.kAlignedDim, textPoint);
}

This code add a new sketch that contains fully constrained rectangle.

 

=====

Freeradical

 Hideo Yamada

 

=====
Freeradical
 Hideo Yamada
https://www.freeradical.jp