×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Macro to link points to CG point

Macro to link points to CG point

Macro to link points to CG point

(OP)
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 !

RE: Macro to link points to CG point

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?

RE: Macro to link points to CG point

(OP)
Hi little Little Cthulhu,

Yes CG = COG sorry ^^
And about the part does new line needs to be created, it doesn't matter.
THx :)

RE: Macro to link points to CG point

(OP)
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 --> vba

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 

RE: Macro to link points to CG point

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members! Already a Member? Login


Resources

Low-Volume Rapid Injection Molding With 3D Printed Molds
Learn methods and guidelines for using stereolithography (SLA) 3D printed molds in the injection molding process to lower costs and lead time. Discover how this hybrid manufacturing process enables on-demand mold fabrication to quickly produce small batches of thermoplastic parts. Download Now
Design for Additive Manufacturing (DfAM)
Examine how the principles of DfAM upend many of the long-standing rules around manufacturability - allowing engineers and designers to place a part’s function at the center of their design considerations. Download Now
Taking Control of Engineering Documents
This ebook covers tips for creating and managing workflows, security best practices and protection of intellectual property, Cloud vs. on-premise software solutions, CAD file management, compliance, and more. Download Now

Close Box

Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close