Continue to Site

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

Status
Not open for further replies.

UnspokenOwl

New member
Jan 5, 2013
16
US
I just bought the Emmett Ross book VB Scripting for CATIA V5 & i'm slowly working my way through learning macros.

I'm trying to create a macro that will Modify a set of points with a coordinate point type definition by a set value.

My Goal would be:

1) Macro looks at whatever geometric set is active
2) Macro would mofify all points in that parts geometric set by 5x, 5y, or 5z.

Something like that is my goal. If anyone has any input or parts of other macro's that would contain some helpful chunks or code, anything to help out would be greatly appreciated. I'm really looking forward to learning more about macro's.

Eventually I hope to get good enough to have a form or GUI created that would allow me to manually add the inputs outside of the source code in the macro having it predefined. One step at a time though :) I haven't used VB is about 15 years, so i'm pretty rusty.

Thanks again for any input.
 
Replies continue below

Recommended for you

Hi,

You need to specify more...

Do you work in a part or a product? Do you want to create the macro in a CATScript or catvba (I suppose you want to be in a catvba since you want a GUI)? Or maybe do you want to create the macro working with CATIA from an EXCEL file (code in EXCEL)? Do you want to be able to select a GS or this GS does have a specific name and can be specified in code?

Anyway, check also this link



Regards
Fernando

 
Yeah it would be moving points inside a geometric set of a .catpart.

And yea I am trying to do it in a catvba.

I didn't know anything about a code from excel. Running it just from CATIA directly would be just fine. I'm pretty open to whatever idea's people can share.

I would like to try to make the macro be ran on the geometric set i select. Worse can scenareo I could try to make all the geometric sets named the same thing, but a selection would be prefered as right now all the geometric sets are named different things.

Thanks for the link also.

 
I'm kinda stuck.

I have been able to get a macro to create new points & move them + a determined amount. But not move an existing point.

The logic would be.

1) User Select a Geometric Set in a CatPart
2) Move 1st point in geometric set by value x
3) Move 2nd Point in geometric set by value x
4) Repeat for all points in the geometric set

Eventually once I get that figured out, I can work to make a form where I can enter the value, but until then.. it can be predefined in the code.

I tried to figure it out with the macro recorder, but it only comes up with creating a new point.
 
Generally speaking, if someone wants help with macros, should post what they tried until that moment. I agree sometimes can be not so much but in this way we can show what mistakes were done or which could be direction to search a solution.

Bellow is a solution in CATScript, it would be great if you can show us your catvba with form, maybe inspired from what I'm posting here (but not necessary, a problem can be solved in few ways from my experience) .

Code:
Language="VBSCRIPT"
Sub CATMain()

Dim oPartDoc As Part
On Error Resume Next

Set oPartDoc = CATIA.ActiveDocument.Part    

If Err.Number <> 0 Then                   
Message = MsgBox("Sorry, this script works with a CATPart as Active document", vbCritical, "Error")
Exit Sub
End If

    ' What do you want to select
    Dim EnableSelectionFor(0)
    EnableSelectionFor(0) = "HybridBody"

    ' Reset the Selection
    Set sSEL = CATIA.ActiveDocument.Selection
    sSEL.Clear

 ' Define Selection
	MsgBox "Please select the Geometrical Set where you have the points"
    UserSelection = sSEL.SelectElement2(EnableSelectionFor, "Please select another Geometrical Set", False)
  
    ' Evaluation if the selection is correct or not
    If UserSelection <> "Normal" Then
        MsgBox "Error with the selection"
        Exit Sub
Else

'''''''''''''''''''''''''''''''input values for new x,y,z coord, this should be completed, clicking on Cancel is not working
Dim new_x As Double
new_x = "5"
new_x = InputBox ("Please enter value for scaling x coordinate" , "scale x coord point", new_x)

Dim new_y As Double
new_y = "5"
new_y = InputBox ("Please enter value for scaling y coordinate" , "scale y coord point", new_y)

Dim new_z As Double
new_z = "5"
new_z = InputBox ("Please enter value for scaling z coordinate" , "scale z coord point", new_z)
'''''''''''''''''''''''''''''''
    End If
Set ohybridbody = sSEL.Item(1).Value
Set partDocument1 = CATIA.ActiveDocument

Dim selection1 As Selection
Set selection1 = partDocument1.Selection

selection1.Search "CATPrtSearch.Point,sel"
''''''''''''''''''''''''''''''''A loop to go thru each selected points and change it's name, just for fun
intNbSelected = selection1.Count2
If intNbSelected > 0 Then
For intIndex = 1 to intNbSelected
selection1.Item2(intIndex).Value.Name = "Point." &intIndex
Next
End If
''''''''''''''''''''''''''''''' 
''''''''''''''''''''''''''''''' New Geometrical Set where where points will be created
 Set part1 = partDocument1.Part
  Set hybridBodies1 = part1.HybridBodies
  Set hybridBody1 = hybridBodies1.Add()
  Set hybridShapeFactory1 = part1.HybridShapeFactory
''''''''''''''''''''''''''''''' 
''''''''''''''''''''''''''''''' Here we are geting the old coord values
	Dim listOfTypes(1)
	listOfTypes(1)="Point"

	Dim myDocument
	Set myDocument = CATIA.ActiveDocument

	Dim mySelection As Selection
	Set mySelection = myDocument.Selection

	Dim number
	number = mySelection.Count
    
              MsgBox "Number of selected points: "& number 'Control message, better to comment after testing

  Dim oPointCoord(2) as CATSafeVariant  
  Dim oSelElem as Object 
  
	Dim selectedElement As SelectedElement
For i=1 to number
    Set selectedElement = mySelection.Item(i)
            MsgBox selectedElement
    
   Set oSelElem = mySelection.Item(I) 
   oSelElem.Value.GetCoordinates (oPointCoord)
''''''''''''''''''''''''''''''new coord values
            x=oPointCoord(0)*new_x
            y=oPointCoord(1)*new_y
            z=oPointCoord(2)*new_z

            MsgBox  "Old coord - " & oPointCoord(0) &  ";" & oPointCoord(1) &  ";" & oPointCoord(2) & vbCrlf & "New coord - " & x  &  ";" & y &  ";" & z  'Control message, better to comment after testing
''''''''''''''''''''''''''''''' 
''''''''''''''''''''''''''''''' Here we are creating new points with the new coord values
    Set hybridShapePointCoord1 = hybridShapeFactory1.AddNewPointCoord(x, y, z)
    hybridBody1.AppendHybridShape hybridShapePointCoord1
    part1.InWorkObject = hybridShapePointCoord1
    part1.Update

Next

End Sub

Regards
Fernando

 
Thanks Ferdo i'll look at that and try it out. The macro recorder is not very good to use, just very basic things and doesn't seem to do loops or the code for exactly what you did in session. I try to share here what I can for knowledge I am comfortable with and I appreciate everyones input and help. I really was looking for bit of code or thoughts on parts of the code I could piece together.

Thank you and when I can get more in depth with it I will share more also. I will work to do it with a form & hopefully can make it work soon. Thank you
 
I was playing with your code, had lots of good things I might be able to tweak and use. It still creates new points, not move original point. Thank you again, I will study it to see how I can make it work with small change.
 
Unfortunately not everything is exposed in documentation and the macro recorder is many times not useful indeed...no way to see loops there, you have to search in documentation when you have problems like this.

And by the way, some workbenches are more "friendly" than others , so don't be surprised if you will get nothing in recorder :) .

Regards
Fernando

 
Hi Ferdo,

The problem is that the original points have other parts that are contextually linked to them. So they must be the same point. The code you provided was a great start though and much of the stuff is very very useful.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor