Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Macro to link points to CG point

Status
Not open for further replies.

Kikoupe

Mechanical
Jul 7, 2021
17
FR
Hi guys,

I would like to have a macro to :
1- create a center point from a user selection curve
2- Create a CG point from a user selection part
3- Link a selection point to a CG point by line

Do you think it's possible ?

Thx for your help !
 
Replies continue below

Recommended for you

What do you mean by "CG"? Center of Gravity - "COG"?
In what part does new line needs to be created: the one that contains curve or the one used to obtain CG?
 
Hi little Little Cthulhu,

Yes CG = COG sorry ^^
And about the part does new line needs to be created, it doesn't matter.
THx :)
 
I got this code that i tried but it's doesn't work very well.

Could you improve it ?

In fact, the sName don't work cause it's have a prefix "Selection_" that why i delete it, but the suffix change... and it's change following the environment that the part have been created...
Do you think I could call the curve selected as geometry ? or object ...?

Thx guys,

Code:
Option Explicit

Sub CATMain()

Dim USel As Selection
Dim USelLB
Dim InputObject(0)
Dim oStatus
Dim i As Integer
Dim sName As String


InputObject(0) = "AnyObject" 'selection filter forces user to select specific objects, AnyObject allows selection of any object

Set USel = CATIA.ActiveDocument.Selection
Set USelLB = USel

MsgBox "Please select fix points." & vbCrLf & "Select a curve only !" & vbCrLf & "Presse escape to cancel the process."

USel.Clear 'clear the selection before making a selection

oStatus = USelLB.SelectElement3(InputObject, "Select objects to list names", True, CATMultiSelTriggWhenUserValidatesSelection, False)
    
    Dim partDocument1 As Document
    Set partDocument1 = CATIA.ActiveDocument

    Dim part1 As Part
    Set part1 = partDocument1.Part

    Dim hybridBodies1 As HybridBodies
    Set hybridBodies1 = part1.HybridBodies

    Dim hybridBody1 As HybridBody
    Set hybridBody1 = hybridBodies1.Add()
    hybridBody1.Name = "Here the result"
    
    Dim hybridShapeFactory1 As HybridShapeFactory
    Set hybridShapeFactory1 = part1.HybridShapeFactory
    
    If (oStatus = "Cancel") Then 'User hit esc on keyboard
       MsgBox "Macro canceled by user"
       Exit Sub
    Else 'Loop through selected objects and list names
       For i = 1 To USel.Count
        
        sName = USel.Item(i).Value.Name
        sName = Replace(sName, "Selection_", "")
        MsgBox (sName)
           
        
        Dim reference1 As Reference
        Set reference1 = part1.CreateReferenceFromBRepName(sName, part1)
        'Set reference1 = part1.CreateReferenceFromGeometry()
        'Set reference1 = part1.CreateReferenceFromName()
        'Set reference1 = part1.CreateReferenceFromObject()
        
        
        Dim hybridShapePointCenter1 As HybridShapePointCenter
        Set hybridShapePointCenter1 = hybridShapeFactory1.AddNewPointCenter(reference1)
        
        hybridBody1.AppendHybridShape hybridShapePointCenter1
        hybridShapePointCenter1.Name = "Point " & i
        'MsgBox hybridShapePointCenter1.Name
        
        part1.InWorkObject = hybridShapePointCenter1
        
        part1.Update
    
       Next
    End If

USel.Clear

End Sub
 
To work with generative geometry (edges, vertices) just cast selected element to Reference:

Code:
Dim refObject as Reference
set refObject = USel.Item(i).Value

If you're not sure what's selected (generated geometry or feature in specification tree) you can use error handling:

Code:
Dim refObject as Reference
on error resume next
  set refObject = USel.Item(i).Value
on error goto 0
if refObject is Nothing then
  set refObject = USel.Item(i).LeafProduct.ReferenceProduct.Parent.Part.CreateReferenceFromObject(USel.Item(i).Value)
end if

As far as I can see there are a few questionable issues in your script:
1. Using "AnyObject" as selection filter allows user to select literally anything in model or specification tree, which doesn't sound right since you want to work with "a user selection curve".
2. To get COG of a geometry we use Measurable:
Code:
Dim doc: set doc = USel.Item(i).LeafProduct.ReferenceProduct.Parent
Dim meas: set meas = doc.GetWorkbench("SPAWorkbench").GetMeasurable(refObject)
ReDim cog(2)
meas.GetCOG cog
3. To get COG of a part we use Inertia:
Code:
ReDim cog(2)
USel.Item(i).LeafProduct.GetTechnologicalObject("Inertia").GetCOG cog
4. Obtained coordinates have to be translated to single part's axis system since initial parts could be located anywhere in global space. Search forum for global coordinates and transformation math samples.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top