0

I already wrote some Macros in Catia V5 built in Macro Editor, but i can't figure out how to access Catia commands from Excel.

I would like to know how can I create for instance a simple straight cylinder by only having in the excel file cylinder's radius and length.

I want to make different pressure vessels in Catia by entering their diameter and height in Excel and after clicking on a button in that excel sheet the vessel should appear in Catia. I have no problem if I already need to have an open empty part in Catia, but would be best just to have Catia Opened with nothing loaded in it.

If i have the start and see how can I access Catia Methods from Excel I think I can figure out the rest methods needed to complete the vessel, like neck, cap, etc.

Thank you.

Edit: I managed to test some stuff, I got stuck on one problem, I don't know how to set up constraints in Excel vba. I moved the code from Catia vb to Visual Express and managed to make it work there but in excel I don't find a link on how to use a driving dimension.

Here is my code:

Sub Main()

    Dim CATIA As Object
    Set CATIA = GetObject(, "CATIA.Application")

    Set openDocument = CATIA.ActiveDocument

    Set currentPart = openDocument.Part

    Set currentHybridBodies = currentPart.HybridBodies

    Set currentHybridBody = currentHybridBodies.Add()

    Set referenceHybridBody = currentPart.CreateReferenceFromObject(currentHybridBody)

    currentPart.HybridShapeFactory.ChangeFeatureName referenceHybridBody, "GeometricalSet"

    Set partOriginElements = currentPart.OriginElements

    Set plnYZ = currentPart.OriginElements.PlaneYZ

    Set currentGeometricalSet = currentPart.HybridShapeFactory

    Dim currentOffset As Integer
    Dim circleDiameter As Integer
    Dim cylinderLength As Integer

    currentOffset = 0

    circleDiameter = Range("B2").Value

    cylinderLength = Range("B3").Value

    Call CreateCylinder(0, 0, circleDiameter, cylinderLength, currentOffset)

    currentPart.Update

    currentOffset = currentOffset + cylinderLength

    circleDiameter = Range("B5").Value

    cylinderLength = Range("B6").Value


    Call CreateCylinder(0, 0, circleDiameter, cylinderLength, currentOffset)

    openDocument.Part.Update

    currentOffset = currentOffset + cylinderLength

    circleDiameter = Range("B8").Value

    cylinderLength = Range("B9").Value

    Call CreateCylinder(0, 0, circleDiameter, cylinderLength, currentOffset)

    currentPart.Update

    currentOffset = currentOffset + cylinderLength

    CATIA.ActiveWindow.ActiveViewer.Reframe

End Sub




Sub CreateCylinder(iCenterX, iCenterY, iDiameter, iLength, iPlaneOffset)

    Set CATIA = GetObject(, "CATIA.Application")
    Set openDocument = CATIA.ActiveDocument
    Set currentPart = openDocument.Part
    Set plnYZ = currentPart.OriginElements.PlaneYZ
    Set currentGeometricalSet = currentPart.HybridShapeFactory

    Set planeOffset1 = currentGeometricalSet.AddNewPlaneOffset(plnYZ, iPlaneOffset, False)

    Set currentHybridBody = currentPart.HybridBodies.Item("GeometricalSet")

    currentHybridBody.AppendHybridShape (planeOffset1)

    openDocument.Part.Update

    Set currentBodies = currentPart.Bodies

    Set currentBody = currentBodies.Add()

    Set currentSketch = currentBody.Sketches.Add(planeOffset1)

    Dim Factory2D As Object

    Set Factory2D = currentSketch.OpenEdition

    Set geometricElements1 = currentSketch.GeometricElements

    Dim axis2D1 As Object

    Set axis2D1 = geometricElements1.Item("AbsoluteAxis")

    Dim line2D1 As Object

    Set line2D1 = axis2D1.GetItem("HDirection")

    Dim line2D2 As Object

    Set line2D2 = axis2D1.GetItem("VDirection")

    Set currentCircle = Factory2D.CreateClosedCircle(iCenterX, iCenterY, iDiameter / 2)

    Dim point2D1 As Object

    Set point2D1 = axis2D1.GetItem("Origin")

    Dim constraints1 As Object

    Set constraints1 = currentSketch.Constraints

    Dim reference2 As Object

    Set reference2 = currentPart.CreateReferenceFromObject(currentCircle)

    Dim constraint1 As Object

    Set constraint1 = constraints1.AddMonoEltCst(catCstTypeRadius, reference2)

    Dim catCstModeDrivingDimensions As Object

    'Set constraint1.Mode = catCstModeDrivingDimensions  'Here I get the error

    Dim iRadius As Double

    iRadius = iDiameter / 2       

    currentCircle.CenterPoint = point2D1

    currentSketch.CloseEdition

    Dim newPad As Object

    Set newPad = currentPart.ShapeFactory.AddNewPad(currentSketch, iLength)

End Sub

On that commented line I get error 438: Object doesn't support this property or method.

The code works without that line, but the geometric elements are not constrained, instead they appear fixed and I don't want this.

That code works fine in Visual Express and put all the constraints where they should be.

Can anyone tell me how to link that Catia command to excel vb.

This is from v5 automation documentation:

enum CatConstraintMode { catCstModeDrivingDimension, catCstModeDrivenDimension }

Also any feedback on my code is appreciated.

Thank you.

excel sheet

Edit: The only references I seem to be allowed to import are these: references

If I select others I receive an error: Error in loading DLL. Catia is installed on a server but I can point to it using Browse... still I'm not sure what to select there because there are a lot of files.

Florin Ics
  • 7
  • 1
  • 8
  • You need to add the CATIA reference libraries. See http://catiav5automation.blogspot.com/2013/05/introduction-to-catia-v5-automation.html about halfway down the page (item 2b) – OpiesDad Mar 24 '16 at 16:39
  • Thank you for your answer, you have an interesting blog, so bad that it didn't appear on google search. – Florin Ics Mar 24 '16 at 17:21
  • It's not my blog; I found it in a google search...I don't even know what CATIA is to be honest. – OpiesDad Mar 24 '16 at 17:30
  • Hello Again, CATIA is a 3d Computer Aided Design software used for different industries like Aerospatial, Automotive, Naval, etc. The problem is i can't start CATIA the way it's shown on that blog because i have it installed on a server. I also can't access CATIA commands in Excel. I tried on a computer with catia installed locally and it works. Any ideas? – Florin Ics Mar 25 '16 at 16:16
  • You're likely going to need to install the CATIA reference libraries at the very least. You could try going into the VBA references for the one that has CATIA installed locally. Look at the location of the CATIA reference library files (should be .tlb). Copy these files onto your machine, and then go into the references on your machine and use the "browse" option to add these references. That should at least get you the library so that you can use the commands in excel. – OpiesDad Mar 25 '16 at 20:12
  • I'm not sure how to fix the other problem, though. – OpiesDad Mar 25 '16 at 20:16
  • The libraries are already present in references, but when i try to add them it says that the dlls are missing. Where exactly should I copy the files on my machine? I browsed to catia's installed location on the server and locate the .tlb but that didn't fix the problem. I will try to use shell function to see if it will work, since it knows the executable path. – Florin Ics Mar 26 '16 at 11:09
  • See if there are any .dll files in CATIA and try those instead of the .tlb file. Honestly, this is outside my league at this point. I found this http://www.vbforums.com/showthread.php?600579-Can-t-set-reference-to-tlb-files which has a link that I can't vouch for and appears a bit old, but may help. – OpiesDad Mar 28 '16 at 15:53
  • @FlorinIcs Which version of CATIA V5 are you using? – vcp Mar 30 '16 at 05:47

1 Answers1

3

Here is a code to create a bolt. Paste the code in a module in Excel and assign a button to the macro.

Sub Button1_Click()
  Dim CATIA As Object
    
  'Get CATIA or Launch it if necessary.
  On Error Resume Next
  Set CATIA = GetObject(, "CATIA.Application")
  If CATIA Is Nothing Then
     Set CATIA = CreateObject("CATIA.Application")
    CATIA.Visible = True
  End If
  On Error GoTo 0
  
  ' Add a new Part
  Set MyDocument = CATIA.documents.Add("Part")
  Set PartFactory = MyDocument.part.ShapeFactory  ' Retrieve the Part Factory.
  Set MyBody1 = MyDocument.part.Bodies.Item("PartBody")
  CATIA.ActiveDocument.part.InWorkObject = MyBody1 ' Activate "PartDesign"
  
' Creating the Shaft
  Set ReferencePlane1 = MyDocument.part.CreateReferenceFromGeometry(MyDocument.part.OriginElements.PlaneYZ)
  
  ' Create the sketch1 on ReferencePlane1
  Set Sketch1 = MyBody1.Sketches.Add(ReferencePlane1)
  Set MyFactory1 = Sketch1.OpenEdition() ' Define the sketch


  
  h1 = Range("E6").Value ' height of the bolt
  h2 = Range("E7").Value ' total height
  r1 = Range("E8").Value ' external radius
  r2 = Range("E9").Value ' Internal radius
  s1 = Range("E10").Value ' Size of the chamfer
  
  Set l101 = MyFactory1.CreateLine(0, 0, r1 - 20, 0)
  Set l102 = MyFactory1.CreateLine(r1 - 20, 0, r1, -20)
  Set l103 = MyFactory1.CreateLine(r1, -20, r1, -h1 + 20)
  Set l104 = MyFactory1.CreateLine(r1, -h1 + 20, r1 - 20, -h1)
  Set l105 = MyFactory1.CreateLine(r1 - 20, -h1, r2, -h1)
  Set l106 = MyFactory1.CreateLine(r2, -h1, r2, -h2 + s1)
  Set l107 = MyFactory1.CreateLine(r2, -h2 + s1, r2 - s1, -h2)
  Set l108 = MyFactory1.CreateLine(r2 - s1, -h2, 0, -h2)
  Set l109 = MyFactory1.CreateLine(0, -h2, 0, 0)
  Sketch1.CenterLine = l109
  
  Sketch1.CloseEdition
  Set AxisPad1 = PartFactory.AddNewShaft(Sketch1)
  
' Creating the Pocket
  Set ReferencePlane2 = MyDocument.part.CreateReferenceFromGeometry(MyDocument.part.OriginElements.PlaneXY)
    
  ' Create the sketch2 on ReferencePlane2
  Set Sketch2 = MyBody1.Sketches.Add(ReferencePlane2)
  Set MyFactory2 = Sketch2.OpenEdition() ' Define the sketch
  D = 1 / 0.866
  
  Set l201 = MyFactory2.CreateLine(D * 100, 0, D * 50, D * 86.6)
  Set l202 = MyFactory2.CreateLine(D * 50, D * 86.6, D * -50, D * 86.6)
  Set l203 = MyFactory2.CreateLine(D * -50, D * 86.6, D * -100, 0)
  Set l204 = MyFactory2.CreateLine(D * -100, 0, D * -50, D * -86.6)
  Set l205 = MyFactory2.CreateLine(D * -50, D * -86.6, D * 50, D * -86.6)
  Set l206 = MyFactory2.CreateLine(D * 50, D * -86.6, D * 100, 0)

  ' Create a big circle around the form to get a Hole
  Set c2 = MyFactory2.CreateClosedCircle(0, 0, 300)
  
  Sketch2.CloseEdition
  Set AxisHole2 = PartFactory.AddNewPocket(Sketch2, h1)
  
  MyDocument.part.Update


End Sub

enter image description here

https://www.dropbox.com/s/rjuadpmsso8fe1p/20160423_2208_36.avi?dl=0

ferdo
  • 178
  • 4
  • Thank you for the code ferdo. I will try and see how to add constraints to each element. – Florin Ics Mar 26 '16 at 21:30
  • 1
    I really don't like On Error Resume Next checks especially when there is a solution without it! in this case: If GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2").ExecQuery("SELECT * FROM Win32_Process Where Name LIKE 'CNEXT%'") Then Exists – tsolina Mar 29 '16 at 09:01
  • Agree with you tsolina. Even belter to check if you have more then one session opened and warn the user about this. – ferdo Mar 29 '16 at 18:17
  • Out of curiosity I tried to run the code but get an error message "does not sppport this propet" at line `Set MyDocument = CATIA.documents.Add("Part")`. Anyway +1 for this answer. – vcp Mar 30 '16 at 06:33
  • @vcp Catia V5 R19. – Florin Ics Mar 30 '16 at 10:41
  • For me is working fine in an Excel module, even in r25. – ferdo Mar 30 '16 at 19:16
  • No one able to answer my edited question? Or point me to some documentation? – Florin Ics Apr 18 '16 at 08:53
  • I found this information here: http://win32com.goermezer.de/content/view/214/282/ but how do I access that constant 0 from excel? – Florin Ics Apr 22 '16 at 07:29
  • Please check the video screen capture in my answer, I don't think is something wrong with your code, maybe you don't have the right references....if is not working for you after checking those references, I'll post the code. – ferdo Apr 23 '16 at 19:21
  • Thank you, I managed to fix that with: constraint1.Mode = 0. For references please see the edit. – Florin Ics Apr 28 '16 at 12:55