Length of Face Cylinder

Length of Face Cylinder

Anonymous
Not applicable
1,766 Views
8 Replies
Message 1 of 9

Length of Face Cylinder

Anonymous
Not applicable

I am looking at the Cylinder object, from a Face geometry. Wonder why Length is not a part of the Object properties?
Its possible to get the Length from the two vertices. Just wondering, if I have overlooked something...
Facelength.png

0 Likes
Accepted solutions (1)
1,767 Views
8 Replies
Replies (8)
Message 2 of 9

J-Camper
Advisor
Advisor

I think you have to access length from the AxisVector, try this:

oCylinder.AxisVector.AsVector.Length

 

0 Likes
Message 3 of 9

Anonymous
Not applicable

Thanks, but the AxisVector is a UnitVector, hence it will always be 1.

0 Likes
Message 4 of 9

J-Camper
Advisor
Advisor

Are you working with perfect cylinders? You could calculate the height:

Dim PickThis As Face = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFaceCylindricalFilter, "Select")
If IsNothing(PickThis) Then Exit Sub ' If nothing gets selected then we're done
Try
MessageBox.Show(PickThis.Evaluator.Area/((PickThis.Geometry.Radius * 2) * Math.PI)/2.54, "Height [in]")
Catch: End Try

 

0 Likes
Message 5 of 9

Anonymous
Not applicable

No, it is not an perfect cylinder. So I figured it out by traversing through all the Edges. Only Edges Normal to the direction, can be Circles or Arcs. (I assume...).

Here is the final code:

Sub SimplifyCylinder()
    ' This utility will create a new Cylinder, removing all internal voids.
    Dim oT As TransientGeometry
    Set oT = ThisApplication.TransientGeometry
    Dim oDoc As PartDocument
    Set oDoc = ThisApplication.ActiveEditDocument
    Dim oPartDef As PartComponentDefinition
    Set oPartDef = oDoc.ComponentDefinition
    
    Dim oFace As Face
    Set oFace = ThisApplication.CommandManager.Pick(kPartFaceCylindricalFilter, "Select circular face")

    If Not oFace Is Nothing Then
        Dim oCylinder As Cylinder: Set oCylinder = oFace.geometry
        Dim oWorkPlane As WorkPlane
        Dim xdir As UnitVector
        Dim ydir As UnitVector
        Set xdir = oCylinder.AxisVector
        ' Create an arbitrary Vector, just so we can create the Normal plane.
        Dim oPnt As Point
        ' Just add a random point to ensure we will always find a Plane.
        Set oPnt = ThisApplication.TransientGeometry.CreatePoint(10.974769876, 45.574764, 10.743238787)
        Dim v1 As Vector
        Set v1 = oCylinder.BasePoint.VectorTo(oPnt)
        Set ydir = xdir.CrossProduct(v1.AsUnitVector)
        Set oWorkPlane = oPartDef.WorkPlanes.AddFixed(oCylinder.BasePoint, xdir, ydir, True)
        
        Dim oEdge As Inventor.edge
        Dim oCircle As Inventor.Circle
        Dim oArc As Arc3d
        Dim tmpLength As Double, Length As Double
        
        For Each oEdge In oFace.Edges
            If oEdge.GeometryType = kCircularArcCurve Then
                Set oArc = oEdge.geometry
                tmpLength = oCylinder.BasePoint.DistanceTo(oArc.Center)
            ElseIf oEdge.GeometryType = kCircleCurve Then
                Set oCircle = oEdge.geometry
                tmpLength = oCylinder.BasePoint.DistanceTo(oCircle.Center)
            End If
            If tmpLength > Length Then
                Length = tmpLength
            End If
        Next
        Dim oSketch As PlanarSketch
        Set oSketch = oPartDef.Sketches.Add(oWorkPlane)
        Dim oSketchLines As SketchEntitiesEnumerator
        Set oSketchLines = oSketch.SketchLines.AddAsTwoPointRectangle(oT.CreatePoint2d(0, 0), oT.CreatePoint2d(Length, oCylinder.Radius))
        Dim oProfile As Profile
        Set oProfile = oSketch.Profiles.AddForSolid()
        
        Dim oRevolve As RevolveFeature
        Dim oSketchAxis As SketchLine
        Set oSketchAxis = oSketch.SketchLines.AddByTwoPoints(oT.CreatePoint2d(0, 0), oT.CreatePoint2d(10, 0))
        Set oRevolve = oPartDef.Features.RevolveFeatures.AddFull(oProfile, oSketchAxis, kJoinOperation)
        
        oDoc.Update2 True
    End If
End Sub

 

 

0 Likes
Message 6 of 9

JhoelForshav
Mentor
Mentor
Accepted solution

Hi @Anonymous 

I think the best approach would be to use TransientGeometry.GetFarmostPoint(Face, AxisVextor) for the face and the axis vector and then again for the inverted axis vector.

Then you can get the vector between these points and measure its projection on the axis vector.

 

Something like this:

Sub Main
Dim oCylinder As Face = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFaceCylindricalFilter, _
"Pick cylindrical face.")
MsgBox("Length of cylinder: " & GetLengthCylinder(oCylinder))
End Sub
Function GetLengthCylinder(oFace As Face) As Double
	Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
	Dim oUM As UnitsOfMeasure = ThisDoc.Document.UnitsOfMeasure
	Dim oCylinder As Cylinder = oFace.Geometry
	Dim oPoint1 As Point = oTG.GetFarmostPoint(oFace, oCylinder.AxisVector)
	Dim oPoint2 As Point = oTG.GetFarmostPoint(oFace, oTG.CreateUnitVector(-oCylinder.AxisVector.X, -oCylinder.AxisVector.Y, -oCylinder.AxisVector.Z))
	Dim oVector1 As Vector = oPoint1.VectorTo(oPoint2)
	Dim oLength As Double
	oLength = (oUM.ConvertUnits(Abs(oCylinder.AxisVector.AsVector.DotProduct(oVector1)), _
	UnitsTypeEnum.kDatabaseLengthUnits, oUM.LengthUnits))
	Return oLength
End Function

I also used UnitsOfMeasure to convert the database units to the documents length units 🙂

 

Message 7 of 9

Anonymous
Not applicable

That was a pretty good solution! Thanks

Also the UnitOfMeasure.

 

Message 8 of 9

FRFR1426
Collaborator
Collaborator

A face is bounded, but the underlying surface is not bounded, it's a mathematical object. The height of the cylinder is infinite, so there is no height property.

Maxence DELANNOY
Manager
Add-ins development for Autodesk software products
http://wiip.fr
0 Likes
Message 9 of 9

_dscholtes_
Advocate
Advocate

@JhoelForshav 's code is a bit shorter than how I tackled a similar problem a while ago:

  • Create a matrix rotating from cylinder's axis vector to z-axis (how to rotate the cylinder so it stands up);
  • Create a BRep copy of the face and rotate it with the matrix (a copy of the cylinder now stands up);
  • Get the minimum range box info of the BRep copy and calculate z-direction difference (there's the length).
0 Likes