Inventor API, Create Dimension from 2 Workpoints in an IDW

Inventor API, Create Dimension from 2 Workpoints in an IDW

Anonymous
Not applicable
6,690 Views
12 Replies
Message 1 of 13

Inventor API, Create Dimension from 2 Workpoints in an IDW

Anonymous
Not applicable

I've been trying to create a LinearGeneralDimension on an IDW from one Workpoint to another.

 

It crashes when i call the .AddLinear, anybody got this to work?

 

Here is my code snipit.

 

Inventor.DrawingView dv = FindDrawingViewInSheetByName(sheet, "VIEW12");

        if (null != dv)
        {
            Inventor.WorkPoint p1 = FindWorkPointInAssemblyByName(dv.ReferencedDocumentDescriptor.ReferencedDocument as Inventor.AssemblyDocument, "MR1");
            Inventor.WorkPoint p2 = FindWorkPointInAssemblyByName(dv.ReferencedDocumentDescriptor.ReferencedDocument as Inventor.AssemblyDocument, "MR2");

            if (null != p1 && null != p2)
            {
                Inventor.GeometryIntent gi1 = sheet.CreateGeometryIntent(p1, Inventor.PointIntentEnum.kStartPointIntent);
                Inventor.GeometryIntent gi2 = sheet.CreateGeometryIntent(p2, Inventor.PointIntentEnum.kEndPointIntent);

                //Inventor.Point2d textPoint = m_application.TransientGeometry.CreatePoint2d(p1.Point.X - 0.25f, (p1.Point.Y + p2.Point.Y) / 2);
                Inventor.Point2d textPoint = m_application.TransientGeometry.CreatePoint2d(sheet.Width/2, sheet.Height/2);
                Inventor.DimensionStyle st = GetStyleByName(ddoc, "Default - Fraction (ANSI)");
                Inventor.Layer layer = GetDimensionLayer(ddoc);

                sheet.DrawingDimensions.GeneralDimensions.AddLinear(textPoint, gi1, gi2, Inventor.DimensionTypeEnum.kDiametricDimensionType, true, st, layer);
            }
     }

 

Thanks for any insite on this.

 

-Doug

0 Likes
Accepted solutions (1)
6,691 Views
12 Replies
Replies (12)
Message 2 of 13

alewer
Advocate
Advocate

Does it still error if you omit the optional arguments?  Only the geometry intent and text point are required.

 

If you can you provide your FindWorkPointInAssemblyByName() function I can give this a closer look.

0 Likes
Message 3 of 13

Anonymous
Not applicable

All that function does is return a Workpoint by name, but here it is.

 

public static Inventor.WorkPoint FindWorkPointInAssemblyByName(Inventor.AssemblyDocument assemblyDoc, string name)
        {
            if (null == assemblyDoc || null == assemblyDoc.ComponentDefinition || null == assemblyDoc.ComponentDefinition.WorkPoints || assemblyDoc.ComponentDefinition.WorkPoints.Count == 0)
            {
                return null;
            }

            string n2 = name.ToLower();
            foreach (Inventor.WorkPoint wp in assemblyDoc.ComponentDefinition.WorkPoints)
            {
                if (wp.Name.ToLower() == n2)
                {
                    return wp;
                }
            }

            return null;
        }

 

I"ll get your suggestion a shot.

0 Likes
Message 4 of 13

Anonymous
Not applicable

I am having a very similar issue. For scenarios like this where a call to the Inventor API generates an exception, how can I log, trace, or inspect the details of that exception? If I use a simple Try/Catch I still get a generic exception.

0 Likes
Message 5 of 13

Anonymous
Not applicable

trying this....sheet.DrawingDimensions.GeneralDimensions.AddLinear(textPoint, gi1, gi2);

 

resulted in the same error.

 

I have found the part in the assemblyDocument, is there a way (after I use ModelToSheetSpace) to use that Point2D to place a new dimension.. here is my code..

 

Inventor.Point2d pp1 = dv.ModelToSheetSpace(p1.Point);
                                Inventor.Point2d pp2 = dv.ModelToSheetSpace(p2.Point);
                             
                                Inventor.GeometryIntent gi1 = sheet.CreateGeometryIntent(pp1, Inventor.PointIntentEnum.kStartPointIntent);
                                Inventor.GeometryIntent gi2 = sheet.CreateGeometryIntent(pp2, Inventor.PointIntentEnum.kEndPointIntent);

                                Inventor.Point2d textPoint = m_app.TransientGeometry.CreatePoint2d(((pp1.X + pp2.X) / 2) - 6, ((pp1.Y + pp2.Y) / 2));
                                Inventor.DimensionStyle st = Search.GetStyleByName(ddoc, "Default - Fraction (ANSI)");
                                Inventor.Layer layer = Search.GetDimensionLayer(ddoc);

                                //sheet.DrawingDimensions.GeneralDimensions.AddLinear(textPoint, gi1, gi2, Inventor.DimensionTypeEnum.kDiametricDimensionType, true, st, layer);
                                sheet.DrawingDimensions.GeneralDimensions.AddLinear(textPoint, gi1, gi2);

0 Likes
Message 6 of 13

Anonymous
Not applicable

Some other solutions i've thought of:

 

1) Select the Workpoints on the IDW via name, add them to the SelectSet, then generate the dimension.

2) Generate new points in the IDW with the coords i have from the original (Transformed via ModelToSheetSpace).

 

Anybody done either of these?

 

0 Likes
Message 7 of 13

alewer
Advocate
Advocate
Accepted solution

Sorry for the late reply.  It's been a while since I've done something like this, so here goes.  I hope VBA is fine, it is the quickest way for me to prototype:

 

Public Sub DimBtwnWorkpoints()
  'Creates a dimension between two work points
  'Active document is inventor drawing
  'Active sheet has view(s)
  'First view is of assembly
  'First component occurrence of assembly is part
  'Part has two work points
  'Dimension will be created between these two work points
  
  Dim oDrawingDocument As Inventor.DrawingDocument
  Set oDrawingDocument = ThisApplication.ActiveDocument
  
  Dim oSheet As Inventor.Sheet
  Set oSheet = oDrawingDocument.ActiveSheet
  
  Dim oView As Inventor.DrawingView
  Set oView = oSheet.DrawingViews.Item(1)
  
  Dim oAssemblyDoc As Inventor.AssemblyDocument
  Set oAssemblyDoc = oView.ReferencedDocumentDescriptor.ReferencedDocument
  
  'Get the first component occurrence of the assembly
  Dim oComponentOcc As Inventor.ComponentOccurrence
  Set oComponentOcc = oAssemblyDoc.ComponentDefinition.Occurrences.Item(1)
  Dim oPartDocument As Inventor.PartDocument
  Set oPartDocument = oComponentOcc.Definition.Document
  
  'Get the first two work points (WorkPoints.Item(1) is the origin)
  Dim oWorkPoint1 As Inventor.WorkPoint
  Dim oWP2 As Inventor.WorkPoint
  Set oWorkPoint1 = oPartDocument.ComponentDefinition.WorkPoints.Item(2)
  Set oWP2 = oPartDocument.ComponentDefinition.WorkPoints.Item(3)
  
  'Create a proxy for the two work points
  Dim oWorkPointProx1 As Inventor.WorkPointProxy
  Dim oWorkPointProx2 As Inventor.WorkPointProxy
  oComponentOcc.CreateGeometryProxy oWorkPoint1, oWorkPointProx1
  oComponentOcc.CreateGeometryProxy oWP2, oWorkPointProx2
  
  'Include the work points in the drawing view
  oView.SetIncludeStatus oWorkPointProx1, True
  oView.SetIncludeStatus oWorkPointProx2, True
  
  'Now we need to find the two centermarks that represent the work point proxies
  Dim oCenterMark1 As Inventor.Centermark
  Dim oCenterMark2 As Inventor.Centermark
  Dim oCenterMark As Inventor.Centermark
  For Each oCenterMark In oSheet.Centermarks
    If oCenterMark.Attached Then
      If oCenterMark.AttachedEntity Is oWorkPointProx1 Then
        Set oCenterMark1 = oCenterMark
      ElseIf oCenterMark.AttachedEntity Is oWorkPointProx2 Then
        Set oCenterMark2 = oCenterMark
      End If
    End If
  Next
  
  'From the two work points, we create the geometry intent
  Dim oGeomIntent1 As Inventor.GeometryIntent
  Dim oGeomIntent2 As Inventor.GeometryIntent
  Set oGeomIntent1 = oSheet.CreateGeometryIntent(oCenterMark1, Inventor.kPoint2dIntent)
  Set oGeomIntent2 = oSheet.CreateGeometryIntent(oCenterMark2, Inventor.kPoint2dIntent)
  
  'Create a point for the text
  Dim oTextPoint As Inventor.Point2d
  Set oTextPoint = ThisApplication.TransientGeometry.CreatePoint2d()
  oTextPoint.X = (oGeomIntent1.PointOnSheet.X + oGeomIntent2.PointOnSheet.X) / 2
  oTextPoint.Y = (oGeomIntent1.PointOnSheet.Y) + 1
  
  'Create the dimension
  Call oSheet.DrawingDimensions.GeneralDimensions.AddLinear(oTextPoint, oGeomIntent1, oGeomIntent2)
  
  'For extra credit, hide the work points
  oCenterMark1.Visible = False
  oCenterMark2.Visible = False
End Sub

 

This may seem like too much of a self-licking ice cream cone, but...

1-Your geometry intent must be part of the drawing view...this means that your work points must be represented by centermarks.

2-To create centermarks, you need to create proxies for the work points.

 

Let me know if it works.

Message 8 of 13

Anonymous
Not applicable

Exactly what I needed, thank you!

0 Likes
Message 9 of 13

CalvinDay
Enthusiast
Enthusiast

How do you get part workpoints to show up in the drawing?

0 Likes
Message 10 of 13

CalvinDay
Enthusiast
Enthusiast

How come the workpoints don't show up as workpoints.item(2) and workpoints.item(3)

 

I have to use oPartDocument.referencedDocuments.item(1).componentDefinition.workpoints.item(2) ...

 

But then the oComponentOcc.CreateGeometryProxy oWorkPoint1, oWorkPointProx1

  oComponentOcc.CreateGeometryProxy oWP2, oWorkPointProx2 don't work.

 

I have tried Include work feature but that didn't work either.

 

 

0 Likes
Message 11 of 13

Anonymous
Not applicable

Inventor.WorkPointProxy wpp1 = Search.GetWorkPointProxyFromAssemblyByName(masterAssemblyDoc, assemblyDoc, wp1.Name);
Inventor.WorkPointProxy wpp2 = Search.GetWorkPointProxyFromAssemblyByName(masterAssemblyDoc, assemblyDoc, wp2.Name);

if (null != wpp1 && null != wpp2)
{
    view.SetIncludeStatus(wpp1, true);
    view.SetIncludeStatus(wpp2, true);
    Inventor.Centermark cm1 = Search.GetCenterMarkFromProxyObject(sheet, wpp1);
    Inventor.Centermark cm2 = Search.GetCenterMarkFromProxyObject(sheet, wpp2);
    Inventor.GeometryIntent gi1 = sheet.CreateGeometryIntent(cm1, Inventor.IntentTypeEnum.kPoint2dIntent);
    Inventor.GeometryIntent gi2 = sheet.CreateGeometryIntent(cm2, Inventor.IntentTypeEnum.kPoint2dIntent);

    Inventor.Point2d textPoint = m_application.TransientGeometry.CreatePoint2d(((gi1.PointOnSheet.X + gi2.PointOnSheet.X) / 2) - 4, ((gi1.PointOnSheet.Y + gi2.PointOnSheet.Y) / 2));
    sheet.DrawingDimensions.GeneralDimensions.AddLinear(textPoint, gi1, gi2, Inventor.DimensionTypeEnum.kAlignedDimensionType, true, st, layer);

    cm1.Visible = false;
    cm2.Visible = false;
}


public static Inventor.Centermark GetCenterMarkFromProxyObject(Inventor.Sheet sheet, object proxyObject)
{
    foreach (Inventor.Centermark cm in sheet.Centermarks)
    {
        if (!cm.Attached)
        {
            continue;
        }
        if (cm.AttachedEntity == proxyObject)
        {
            return cm;
        }
    }
    return null;
}

0 Likes
Message 12 of 13

CalvinDay
Enthusiast
Enthusiast

I figured it out. When creating iParts, you must include those items on the iclude work features page.

0 Likes
Message 13 of 13

Anonymous
Not applicable

I created advanced code with classes with functions on both part/assembly side and the drawing side that can create ordinate linear diameter angular and radius dimensions from intents. from work planes, points, axes and also faces, vertexes, and edges. (everything is fully automated)

 

If you would like to see this than i can show you and explain you.

contact me on. m.den.ouden@twentbelt.com

0 Likes