【问题标题】:Design a Part in Catia by clicking an Excel Button通过单击 Excel 按钮在 Catia 中设计零件
【发布时间】:2016-03-24 16:33:03
【问题描述】:

我已经在宏编辑器内置的 Catia V5 中编写了一些宏,但我不知道如何从 Excel 访问 Catia 命令。

我想知道如何通过仅在 excel 文件中圆柱体的半径和长度来创建一个简单的直圆柱体。

我想在 Catia 中制作不同的压力容器,方法是在 Excel 中输入它们的直径和高度,然后单击该 Excel 表中的按钮后容器应该出现在 Catia 中。如果我已经需要在 Catia 中打开一个空的部分,我没有问题,但最好只打开 Catia,其中没有加载任何内容。

如果我有开始,看看如何从 Excel 访问 Catia 方法,我想我可以找出完成容器所需的其他方法,如颈部、帽等。

谢谢。

编辑: 我设法测试了一些东西,我遇到了一个问题,我不知道如何在 Excel vba 中设置约束。我将代码从 Catia vb 移到 Visual Express 并设法让它在那里工作,但在 excel 中我找不到有关如何使用驱动尺寸的链接。

这是我的代码:

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

在该注释行上,我收到错误 438:对象不支持此属性或方法。

代码在没有该行的情况下工作,但几何元素不受约束,相反它们看起来是固定的,我不想要这个。

该代码在 Visual Express 中运行良好,并将所有约束放在应有的位置。

谁能告诉我如何将 Catia 命令链接到 excel vb。

这是来自 v5 自动化文档:

枚举 CatConstraintMode { catCstModeDrivingDimension, catCstModeDrivenDimension }

感谢您对我的代码提供任何反馈。

谢谢。

excel sheet

编辑: 我似乎被允许导入的唯一参考是这些: references

如果我选择其他人,我会收到错误:加载 DLL 时出错。 Catia 安装在服务器上,但我可以使用 Browse 指向它...我仍然不确定在那里选择什么,因为有很多文件。

【问题讨论】:

  • 您需要添加 CATIA 参考库。见catiav5automation.blogspot.com/2013/05/… 大约在页面的中间(第 2b 项)
  • 谢谢你的回答,你有一个有趣的博客,太糟糕了,它没有出现在谷歌搜索中。
  • 这不是我的博客;我在谷歌搜索中找到了它......老实说,我什至不知道 CATIA 是什么。
  • 您好,CATIA 是一种 3D 计算机辅助设计软件,用于航空航天、汽车、海军等不同行业。问题是我无法按照该博客上显示的方式启动 CATIA,因为我将其安装在服务器上。我也无法在 Excel 中访问 CATIA 命令。我在本地安装了 catia 的计算机上进行了尝试,它可以正常工作。有什么想法吗?
  • 您可能至少需要安装 CATIA 参考库。您可以尝试进入本地安装了 CATIA 的 VBA 参考。查看 CATIA 参考库文件的位置(应该是 .tlb)。将这些文件复制到您的机器上,然后进入您机器上的引用并使用“浏览”选项添加这些引用。这至少应该为您提供库,以便您可以在 excel 中使用命令。

标签: excel vba macros catia


【解决方案1】:

这是创建螺栓的代码。将代码粘贴到 Excel 中的模块中,并为宏分配一个按钮。

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

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

【讨论】:

  • 感谢您提供代码 ferdo。我会尝试看看如何为每个元素添加约束。
  • 我真的不喜欢 On Error Resume Next 检查,尤其是当没有它的解决方案时!在这种情况下:如果 GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2").ExecQuery("SELECT * FROM Win32_Process Where Name LIKE 'CNEXT%'") Then Exists
  • 同意你的看法,tsolina。甚至可以检查您是否打开了一个以上的会话并警告用户。
  • 出于好奇,我尝试运行代码,但在Set MyDocument = CATIA.documents.Add("Part") 行收到错误消息“不支持此属性”。无论如何 +1 这个答案。
  • @vcp Catia V5 R19.
猜你喜欢
  • 2016-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多