Home > The CATIA Object Model > Two methods to check geometry types

Two methods to check geometry types

January 11, 2010 Leave a comment Go to comments

In this article, I am going to discuss two different methods to interrogate a geometrical feature and understand what type of geometry it is.  By type of geometry I mean point, surface, line, solid and so on.  This is different from the automation object type that you would get from using the Visual Basic TypeName function, which I discussed in a previous article.

This can be incredibly useful in many situations.  I have listed just a couple of examples below:

  • Realize that any intersect (HybridShapeIntersection) object could possibly be a surface, line, point, curve or circle.  Just because you know it is one of these in one of your test scenarios doesn’t mean it always will be in other scenarios.  It is wise to check its geometrical type before proceeding to build some other geometry from it in your code.
  • When prompting for a user selection of some geometry, sometimes you can’t define a perfect filter of what object types should be allowed for selection.  This is because the selection filter is based on automation data types rather than the underlying geometrical data types.  Instead, you might want to define the best filter you can, then check the geometrical type after the selection and then decide what to do if the resultant selection isn’t what you require.

Method 1: Checking the “basic” geometrical type

It may seem like there is a very long list of geometry types that you could create with all of the different wireframe, surface and solid functions in CATIA.  However, in reality all geometrical features will either be one of seven types (or in some cases a combination of them).  I call these seven types, “basic” types and they are:

  1. Point
  2. Curve
  3. Line
  4. Circle
  5. Surface
  6. Plane
  7. Solid or Volume

To get the basic type of a geometrical feature, you just need make a call to the GetGeometricalFeatureType method provided by the HybridShapeFactory object.  This is a simple two step process.

  1. Create a special reference to the object to be checked using a reference object.  The easiest way to accomplish this is to use the CreateReferenceFromObject method provided by the part object. (see example below)
  2. Call the GetGeometricalFeatureType method and pass the reference as an argument. The function will return a number corresponding to the numbered list above.

Method 1:  Example code

In this example, I will check the geometrical type of the first feature that exists in the first geometrical set of a part which is open in its own window and display the result in a MsgBox.  In a real scenario rather than just displaying the result, you would probably take some other actions using if – then statements.

Sub DisplayBasicGeomType()

'This code assumes a part is open in its own window and
'the first geo set contains at least one hybridshape feature

Dim objPart as Part
Dim objHSF As HybridShapeFactory
Dim objHShape As HybridShape
Dim objHShapeRef As Reference
Dim intGeomType As Integer
Dim strGeomType As String
Dim strTitle As String
Dim strMsg As String

'Get at the Part and HybridShapeFactory
Set objPart = CATIA.ActiveDocument.Part
Set objHSF = objPart.HybridShapeFactory

'Get the first shape in the first geo set
Set objHShape = objPart.HybridBodies.Item(1).HybridShapes.Item(1)
Set objHShapeRef = objPart.CreateReferenceFromObject(objHShape)

'Check the geometrical type of the shape
intGeomType = objHSF.GetGeometricalFeatureType(objHShapeRef)

'Convert the integer geom type value to a recognizable string for display
Select Case intGeomType
Case 0
  strGeomType = “Unknown”
Case 1
  strGeomType = “Point”
Case 2
  strGeomType = “Curve”
Case 3
  strGeomType = “Line”
Case 4
  strGeomType = “Circle”
Case 5
  strGeomType = “Surface”
Case 6
  strGeomType = “Plane”
Case 7
  strGeomType = “Solid or Volume”
End Select

'Display the result to the user
strTitle = “GetGeometricalFeatureType() Method”
strMsg = objHShape.Name &“ is a ” &strGeomType &“.”
Msgbox strMsg, 64, strTitle

End Sub

You will see a result that looks something like this…

Method 2: Checking the “specific” geometrical type

This method will provide a little bit more detailed information about the geometry than you can get from the method described above.  For example, that method will always return a value of 5 (Surface) for any surface no matter what it’s actual shape is.  However, a surface might be a cone, a cylinder, a sphere or some other complex surface.  You may encounter a situation where you may require a cylinder surface in order to perform some downstream operation successfully.  This method will provide that extra bit of information.

This method will identify geometry as one of 12 geometrical types:

  1. Other
  2. Volume
  3. Surface
  4. Cylinder
  5. Sphere
  6. Cone
  7. Plane
  8. Curve
  9. Circle
  10. Line
  11. Point
  12. Axis System

To get the specific type of a geometrical feature, you just need to access the GeometryName property provided by the Measurable object.  This is a simple three step process.

  1. Create a special reference to the object to be checked using a reference object.  The easiest way to accomplish this is to use the CreateReferenceFromObject method provided by the part object (see example below).
  2. Get at the SPAWorkbench object (SPace Analysis) and pass the Reference object from step 1 to the GetMeasurable method to define the item to be measured
  3. Check the GeometryName property.  This property will return a number corresponding to the geometrical types listed above.

Method 2:  Example code

In this example I will check the geometrical type of the first feature that exists in the first geometrical set of a part which is open in its own window – same scenario as the previous example.

Sub DisplaySpecificGeomType()

'This code assumes a part is open in its own window and
'the first geo set contains at least one hybridshape feature

Dim objPart As Part
Dim objSPAWkb As Object
Dim objHShape As HybridShape
Dim objHShapeRef As Reference
Dim intGeomType As Integer
Dim strGeomType As String
Dim strTitle As String
Dim strMsg As String

'Get at the Part and the space analysis workbench
Set objPart = CATIA.ActiveDocument.Part
Set objSPAWkb = CATIA.ActiveDocument.GetWorkbench(“SPAWorkbench”)

'Get the first shape in the first geo set
Set objHShape = objPart.HybridBodies.Item(1).HybridShapes.Item(1)
Set objHShapeRef = objPart.CreateReferenceFromObject(objHShape)

'Define the object to be measured
Set objMeasurable = objSPAWkb.GetMeasurable(objHShapeRef)

'Check the geometrical type of the shape
intGeomType = objMeasurable.GeometryName

'Convert the integer geom type value to a recognizable string for display
Select Case intGeomType
Case 0
  strGeomType = “Unknown”
Case 1
  strGeomType = “Other”
Case 2
  strGeomType = “Volume”
Case 3
  strGeomType = “Surface”
Case 4
  strGeomType = “Cylinder”
Case 5
  strGeomType = “Sphere”
Case 6
  strGeomType = “Cone”
Case 7
  strGeomType = “Plane”
Case 8
  strGeomType = “Curve”
Case 9
  strGeomType = “Circle”
Case 10
  strGeomType = “Line”
Case 11
  strGeomType = “Point”
Case 12
  strGeomType = “Axis System”
End Select

'Display the result to the user
strTitle = “GeometryName() Property”
strMsg = objHShape.Name &“ is a ” &strGeomType &“.”
Msgbox strMsg, 64, strTitle

End Sub

This time you will see a result that looks something like this…

Additional Notes

  • Both of these methods will return 0 (unknown) if the object being measured is not a geometrical feature. For example, if a parameter or a geometrical set were used you would get 0 and no error would result. While those are odd cases that should never occur, it is nice to know these methods will not throw an error if you attempt to measure some unexpected object type in your code.
  • When working with solids, both methods can be used successfully on a body object.  In this special case, you are not required to use an actual geometrical feature.
  • The GeometryName method does not work with sketches.  The return value will be 0 (unknown).  However, the GetGeometricalFeatureType method will work correctly with sketches.
  • Beware if the geometry being tested is non-connex (made up of more than one non-connected pieces) or has mixed geometry types (like a single intersect that is a point and a curve).   In these cases, you may want to disassemble the geometry into separate elements representing each domain then get the geometrical type of each individually.  This can be accomplished with the HybridShapeFactory.AddNewDatums() method.

Please take a moment to rate this article…Just click the stars up near the title.

Add to: del.icio.us del.icio.us, StumbleUpon StumbleUpon, Digg Digg, Google Google

  1. Mike McKown
    March 31, 2010 at 7:36 pm

    Mike,

    I stubled across this site from a link on the COE website. This is great info your putting out for all users!

    Now my question is on the last part of your text. How does one find out that they have a non-connex before they seperate them. Example would be a point created using the tangent circle function that has two points as the result.

    Thanks,

    Mike

  2. March 31, 2010 at 8:20 pm

    Hi Mike,

    Thanks for the positive feedback. There are a couple ways to check the number of domains of an element.

    First way is to disassemble the feature using the HybridShapeFactory.AddNewDatums() method. Even though interactively you can choose to disassemble by domain or by cell, in vb it only does it the domain way. This function will return an array containing new dead copies of the geometries so you can check the size of the array to understand the number of domains. If you go this route, and you don’t append these newly generated elements to the tree, you should call HybridShapeFactory.DeleteObjectForDatum on each one to destroy them. If not, that geometry is still in the part (you just can’t see it…CATDUA will catch it as errors)

    The other option is to create an integer parameter on the fly in the part and add a formula to it something like, Integer.1 = NbDomains(Point.32) …I am not in front of CATIA right now, so i think that is the right syntax. Then just get the value of that integer parameter in your script and finally delete the formula and parameter. You should be able to record most of this code…This might be easier to code and has an added advantage – you can use it on solids too, whereas the first option is only good on wireframe/surfaces.

    Just post back if you have trouble and I’ll help you out.

    -Mike

  3. romas
    August 5, 2010 at 6:35 am

    Hi,
    thanks for this outline, very helpful. It’s solved a half of the problem I have though. What I’m trying to do is to get geometric properties of a curve being a result of intersection between a volume and a plane. Your described method indicates that I have actually a curve (which I also see on the screen) but not HybridShapeIntersection, which I get after the intersection operation and this object gives me nothing useful.

    So, the question is, having a reference to a curve, how I cast it into an appropriate object (line/spline/…) to further extract its points etc.?

    Thanks.

    • August 5, 2010 at 3:55 pm

      You can only cast an object to another interface that is implemented by that object. So in the case of a HybridShapeIntersection object, it does not implement a line or spline interface so it is not possible. You can see what interfaces an object implements by looking it up in the CATIA Automation help docs (it is at the top of each page) Your best option might be to use the measurable object to get information about the intersection curve. To get all of the vertex points on the curve you could also perform a search for vertices using the selection object then measure each. Good luck.

  4. sajid
    January 17, 2012 at 1:25 pm

    can any one help in coding for changing point type….
    like point on curve need to be changed to same point in co-ordinates

  5. Jamie
    October 19, 2012 at 2:32 pm

    Very usefull (and great learning resource thank you very much this is my first comment!)
    Does any know how to identify other items such as electrical information?
    I know a harness bundle for example can be indetified as a rib but how do I extract the electrical name ie Bundle name?

  6. Jamie
    October 19, 2012 at 2:33 pm

    Just to add I have tried IOR method but still only gives rib?

  7. R. Martin
    December 17, 2012 at 5:56 pm

    Hi there,
    What would be the typename of a MeasureEdge or MeasureBetween object. I try to select those but can’t find the correct filter type to set SelectElement entry. Thanks!

    • December 18, 2012 at 10:13 pm

      There is no automation object to provide access to the measures that you manually add to the tree in CATIA. However, there is an equivalent automation object that allows you to perform measurements in your scripts. Look at the Measurable object in the SPATypelib

      • R. Martin
        December 18, 2012 at 11:35 pm

        Hi v5vb, thanks for your response. Unfortunatelly, I do not want to perform measurements, just want to get the object. The initial idea was to have a tool capable to read either points or measurements in the tree/GUI, for flexibility. Points in model and the Length parameter under Measurable items are no problem. But looks like is going to be rather difficult to pick the measure edge item after all. Cheers

  8. eric
    May 15, 2013 at 3:32 pm

    searching for GSMGetObjectFromReference i got here and find got info.
    I do have an extract (an edge of a sketch). I can get the ref from HybridShapeExtract.Elem.

    but after that my GSMGetObjectFromReference does not work and return “method ‘AddNewSurfaceDatum’ of ‘HybridShapeFactory’ failed.

    I dim my object as HybridShape, or sketch but same error message

    How can I retrieve the parent object (the sketch) of an Extract ?

    • May 15, 2013 at 8:55 pm

      Hi Eric – you are on the right track but your are just getting the wrong reference. Use Part.CreateReferenceFromObject(Extract) then pass that reference to AddNewSurfaceFromDatum and it should work.
      As you said, the HybridShapeExtract gave you a reference to the parent extracted element (which is your sketch edge) but passing that reference to GSMGetObjectFromReference probably fails because that method is meant to return an object from a direct reference to that object and can’t resolve an object from a BREP reference. Reference objects are very abstract objects and can represent so many things and made from different contexts especially when working with product based references. When you make a call requiring a reference for an argument, it can sometimes be hard to know what it really wants. I know that may sound confusing but hope you understand… -Mike

  9. Girish
    July 25, 2013 at 4:35 pm

    Hi v5vb, Thanks for the info you have been sharing with us. I appreciate your effort and time.
    In connection with your info on checking no. of domains in an element, I have same issue at the moment, as a 1st step I want to find no. of domains in a curve ( I will try with your example approach), but as 2nd step I want to disassemble(AddNewdatum) it based on no. of domains in my macro, as my main objective is to find max distance. can you please share a method on this. – Girish

  1. No trackbacks yet.

Leave a comment