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!

Bounding Box Macro for Catia V5 from Mark Forbes... 1

Status
Not open for further replies.

Bill Lanzilotta

Aerospace
Mar 6, 2018
1
US
Absolute great macro written by Mark.
Was wondering what line in the code needs changing so the output would be in inches and not MM.
Thanks in advance...
Script below...

Sub CATMain()

On Error Resume Next

Dim oPartDoc As PartDocument
Dim oCurrentDoc As Document
Dim oPart As Product
Dim oInertia As Inertia
Dim InputObjectType(1) As Variant
Dim Status As String
Dim oSelection As Variant
Dim bCloseDoc As Boolean

Set oCurrentDoc = CATIA.ActiveDocument

'Exclude Drawings
If Right(oCurrentDoc.Name, 4) = "wing" Then
MsgBox "This function only operates on a part or product"
Exit Sub
End If


Set oSelection = oCurrentDoc.Selection

If Right(oCurrentDoc.Name, 4) = "Part" Then
Set oPart = oCurrentDoc.Product
oSelection.Add oPart
'GoTo RunBBOpenWindow
End If


If Right(oCurrentDoc.Name, 4) = "duct" Then
bCloseDoc = True
InputObjectType(0) = "Part"
InputObjectType(1) = "Product"
oSelection.Clear
Status = oSelection.SelectElement2(InputObjectType, "Pick a Part, Escape to Cancel", False)
If Status = "Cancel" Then Exit Sub
Set oPart = oSelection.Item2(1).LeafProduct.ReferenceProduct
If oPart.Name = oCurrentDoc.Product.Name Then
bCloseDoc = False
'GoTo RunBBOpenWindow
End If
CATIA.StartCommand "open in new window"
End If

RunBBNewWindow:

Set oPart = CATIA.ActiveDocument.Product
Dim oSelection2
Set oSelection2 = CATIA.ActiveDocument.Selection
oSelection2.Add oPart

RunBBOpenWindow:


CATIA.StartCommand "Measure Inertia"

Dim xDim As String
Dim yDim As String
Dim zDim As String

xDim = oPart.Parameters.GetItem("BBLx").Value
yDim = oPart.Parameters.GetItem("BBLy").Value
zDim = oPart.Parameters.GetItem("BBLz").Value

xDim = CStr(Round(xDim, 0))
yDim = CStr(Round(yDim, 0))
zDim = CStr(Round(zDim, 0))

Dim oMatParam As Parameter
Dim sMatParam As String
Set oMatParam = oPart.Parameters.GetItem("Definition")

sMatParam = oMatParam.ValueAsString

Dim oMassParam As Parameter
Dim sMassParam As String
Set oMassParam = oPart.UserRefProperties.GetItem("PDB_MASS")


sMassParam = oMassParam.ValueAsString
If sMassParam = "" Then sMassParam = "0"

If sMassParam = "0" Then
sMassParam = ""
If Right(CATIA.ActiveDocument.Name, 4) = "Part" Then
Set oInertia = oPart.GetTechnologicalObject("Inertia")

Select Case oInertia.Density

Case 0
sMassParam = ""

Case 1000
sMassParam = ""

Case Else
sMassParam = CStr(Round(oInertia.Mass, 1))

End Select

End If

End If


CATIA.StatusBar = "Start Creating Params"

Call SetParam(oPart, "LENGTH", xDim)
Call SetParam(oPart, "WIDTH", yDim)
Call SetParam(oPart, "THICKNESS/DIAMETER", zDim)
Call SetParam(oPart, "MATERIAL", sMatParam)
Call SetParam(oPart, "MASS", sMassParam)


MsgBox "Properties Applied:" & _
vbCrLf & "LENGTH: " & xDim & _
vbCrLf & "WIDTH: " & yDim & _
vbCrLf & "THICKNESS/DIAMETER: " & zDim & _
vbCrLf & "MATERIAL: " & sMatParam & _
vbCrLf & "MASS: " & sMassParam & _
vbCrLf & "Check Results Carefully!", vbOKOnly, oPart.Name

CATIA.DisplayFileAlerts = False
CATIA.StartCommand "Save"
'CATIA.ActiveDocument.Save
CATIA.DisplayFileAlerts = True


If bCloseDoc = True Then CATIA.ActiveDocument.Close

CATIA.StatusBar = "Macro Finished"

End Sub

Sub SetParam(ByRef oPart As Product, Name As String, Value As String)

'On Error GoTo CreateParam
Dim sParam As Parameter
Err.Clear

Set sParam = oPart.UserRefProperties.GetItem(Name)
sParam.ValuateFromString CStr(Value)

'GoTo Finish

CreateParam:
oPart.UserRefProperties.CreateString Name, CStr(Value)

Finish:


End Sub

 
Replies continue below

Recommended for you

Right underneath these sets of values:

Code:
xDim = CStr(Round(xDim, 0))
yDim = CStr(Round(yDim, 0))
zDim = CStr(Round(zDim, 0))


You just need to add in these values:

Code:
'convert units from metric to imperial for box dimensions
xDim = Math.Round((xDim / 25.4), 2) & " in"
yDim = Math.Round((yDim / 25.4), 2) & " in"
zDim = Math.Round((zDim / 25.4), 2) & " in"

And you're good to go.

 
Bill - apologies. I checked that code in VBA, but should have run it standalone in a CATScript.

Try this. Insert this code in the same place suggested above:

Code:
'convert units from metric to imperial for box dimensions
xDim = (Round((xDim/25.4), 2)) '& " in"
yDim = (Round((yDim/25.4), 2)) '& " in"
zDim = (Round((zDim/25.4), 2)) '& " in"

 
Great bit of code.
Thanks Bill for posting and Mark for writing.
Does anyone know why the material name isn't showing? It isn't a problem but would be nice to know.

cheers
Alan
 
Yes, tried a few times with different ones.
It's not an issue, I'm just trying to learn to do this stuff and wondered why.
I thought this code worked like the roughstock command giving you the minimum material size.
Unfortunately it seems to create the stock size from the inertia axis.
Still really handy though.
 
If you have the "core & cavity design" workbench, there is a bounding box function already implemented.

regards,
LWolf
 
I have seen a similar macro that gives you the option to use either the cog axis or by selecting edges to create the axis.
If I remember correctly it encoded so couldn't see how it was done.
Not having a roughstock option in the basic catia software is a bit of a joke. It should be standard.
 
It's been very many years, I did not remember that I had put my name in the header. I don't even remember whether the PortableScriptCenter was the basis for the code, or an inspiration (most likely the former!). But I do remember that this code (as presented) has severe limitations regarding generating data that is actually useful. The only thing I really found useful was to fill in rough values on a table so they would not appear blank. Anything more than that requires actual work and thought [smarty]

Here's the old thread, for reference:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top