Taking the roughest measurements in a view with C#

Taking the roughest measurements in a view with C#

nebi_guler
Enthusiast Enthusiast
1,053 Views
13 Replies
Message 1 of 14

Taking the roughest measurements in a view with C#

nebi_guler
Enthusiast
Enthusiast

Ekran Alıntısı.PNG

I want to make the drawing in 

 

ViewOrientationTypeEnum.kLeftViewOrientation

 

view as on the left, but it is almost impossible to scale correctly through the Curves in

 

DrawingCurves[(object)viewDimensions.ReferencedFile.Referenced Document as DrawingCurvesEnumerator]

 

Can you help me with this? My goal is to get certain measurements.

0 Likes
1,054 Views
13 Replies
Replies (13)
Message 2 of 14

Michael.Navara
Advisor
Advisor

If the model is aligned to it's global coordinate system, you can use RangeBox from referenced model.

In this case you can create helper sketch in DrawingView, draw a line in them and create dimensions of this line.

 

Sub Main()
    Dim drawingView As DrawingView = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kDrawingViewFilter, "Pick drawing view")
    Dim part As PartDocument = drawingView.ReferencedDocumentDescriptor.ReferencedDocument
    Dim rangeBox As Box = part.ComponentDefinition.SurfaceBodies(1).RangeBox

    Dim minPointInSheetSpace As Point2d = drawingView.ModelToSheetSpace(rangeBox.MinPoint)
    Dim maxPointInSheetSpace As Point2d = drawingView.ModelToSheetSpace(rangeBox.MaxPoint)

    Dim drawingViewSketch As DrawingSketch = drawingView.Sketches.Add()

    Dim minPointInSketchSpace As Point2d = drawingViewSketch.SheetToSketchSpace(minPointInSheetSpace)
    Dim maxPointInSketchSpace As Point2d = drawingViewSketch.SheetToSketchSpace(maxPointInSheetSpace)

    drawingViewSketch.Edit()

    Dim diagonalLine as SketchLine = drawingViewSketch.SketchLines.AddByTwoPoints(minPointInSketchSpace, maxPointInSketchSpace)
    diagonalLine.SketchOnly = True

    drawingViewSketch.ExitEdit()

    Dim sheet As Sheet = drawingView.Parent

    Dim diagonalLineIntent = sheet.CreateGeometryIntent(diagonalLine)

    Dim textPoint1 As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(drawingView.Position.X, drawingView.Position.Y - drawingView.Height / 2 - 1)
    Dim textPoint2 As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(drawingView.Position.X + drawingView.Width / 2 + 1, drawingView.Position.Y)

    sheet.DrawingDimensions.GeneralDimensions.AddLinear(textPoint1, diagonalLineIntent, DimensionType := DimensionTypeEnum.kHorizontalDimensionType)
    sheet.DrawingDimensions.GeneralDimensions.AddLinear(textPoint2, diagonalLineIntent, DimensionType := DimensionTypeEnum.kVerticalDimensionType)
End Sub
Message 3 of 14

nebi_guler
Enthusiast
Enthusiast

I will try . And I will get back to you about it. Thank you . @Michael.Navara 

0 Likes
Message 4 of 14

nebi_guler
Enthusiast
Enthusiast

Ekran Alıntısı5.PNG

part.ComponentDefinition.SurfaceBodies[1].RangeBox

this line comes empty.  I want to tell exactly @Michael.Navara 

0 Likes
Message 5 of 14

Michael.Navara
Advisor
Advisor

Sorry I don't understand what happens. Can you upload a test model?

0 Likes
Message 6 of 14

nebi_guler
Enthusiast
Enthusiast

Ekran Alıntısı8.PNGEkran Alıntısı9.PNG

Of course @Michael.Navara 

0 Likes
Message 7 of 14

Michael.Navara
Advisor
Advisor

I want the Inventor part model, but the screenshot explains a lot.

When you change type of variable part to AssemblyDocument, you need to update rest of your code to work with assemblies and its occurrences and so on.

0 Likes
Message 8 of 14

abdullah_elq
Advocate
Advocate

FYI @nebi_guler, at my last company we used DraftAid. Their add-in is really good at making these types of drawings.

0 Likes
Message 9 of 14

nebi_guler
Enthusiast
Enthusiast
I dont understand what do you mean " you need to update rest of your code to work with assemblies and its occurrences and so on." I have already written the code you wrote to me.
0 Likes
Message 10 of 14

Michael.Navara
Advisor
Advisor

Not exactly. Compare line 3 in my code and line 1027 in your. I declare variable part as PartDocument you declare variable part as AssemblyDocument.

 

I expect you want to create drawing from part not from assembly.

0 Likes
Message 11 of 14

nebi_guler
Enthusiast
Enthusiast

I will replace it and test it immediately

0 Likes
Message 12 of 14

nebi_guler
Enthusiast
Enthusiast

Ekran Alıntısı10.PNG

0 Likes
Message 13 of 14

yan.gauthier
Advocate
Advocate

Hi, 

 

Here's my code I use to get the bounding volume from a Part. The BoundingVolume is a simple custom class that contains the X,Y,Z dimensions with the Volume.

 

       public static BoundingVolume GetOrientedRangingBox(PartDocument partDocument)
       {
           //assume partDocument exists

           var bodies = partDocument.ComponentDefinition.SurfaceBodies.Cast<SurfaceBody>();

           TransientBRep transientBRep = AddInServer.InvApp.TransientBRep;
           SurfaceBody? combinedBodies = null;
           if (bodies != null && bodies.FirstOrDefault() is SurfaceBody baseBody)
           {
               combinedBodies = transientBRep.Copy(baseBody);

               if (bodies.Count() > 1)
               {
                   foreach (SurfaceBody body in bodies.Skip(1))
                   {
                       if (body.IsSolid)
                       {
                           transientBRep.DoBoolean(combinedBodies, body, BooleanTypeEnum.kBooleanTypeUnion);
                       }
                   }
               }
           }
           
           if (combinedBodies is null) return new BoundingVolume(0, 0, 0);

           var orientedBox = combinedBodies.OrientedMinimumRangeBox;

           //Autodesk internal dimensions are always centimeter
           double[] dimensions = new double[] {orientedBox.DirectionOne.Length * 10, orientedBox.DirectionTwo.Length * 10, orientedBox.DirectionThree.Length * 10}.OrderByDescending(x => x).ToArray();

           return new BoundingVolume(dimensions[0], dimensions[1], dimensions[2]);

       }

 This method cares not for your part to be aligned with the axis system. 

0 Likes
Message 14 of 14

nebi_guler
Enthusiast
Enthusiast
Thank you i will try and check.
0 Likes