您的位置:首页 > 其它

关于CATIA V5二次开发中实体“体积”测量的宏问题

2016-09-04 16:57 253 查看
尊重原创,转载请注明出处! mansin_csa@163.com

实际上,CATIA在很多情况下并不能成功录制宏,比如,在自由曲面设计模块中,所有功能均不能录制宏。还有在测量功能中也不能录制宏。事实上,查阅V5Automation知道,其实是有关于测量功能的方法的。如下图所示:



在Measurable中就可以通过Volume方法来获得目标的体积。在实际编程中,还是需要很多前提才能成功使用该功能的。

【特别注意】:在CATIA GUI中是可以通过在“选项”中更改测量单位的。但在宏程序中所获得的体积的单位是m^3。

以下给出一个测量一个拟柱体体积的完整的宏程序。

Sub CATMain()
Set documents1 = CATIA.Documents
Set partDocument1 = documents1.Add("Part")
Set part1 = partDocument1.Part
Set bodies1 = part1.Bodies
Set body1 = bodies1.Item("零件几何体")
Set sketches1 = body1.Sketches
Set originElements1 = part1.OriginElements
Set reference1 = originElements1.PlaneZX
Set sketch1 = sketches1.Add(reference1)
Dim arrayOfVariantOfDouble1(8)
arrayOfVariantOfDouble1(0) = 0.000000
arrayOfVariantOfDouble1(1) = 0.000000
arrayOfVariantOfDouble1(2) = 0.000000
arrayOfVariantOfDouble1(3) = -1.000000
arrayOfVariantOfDouble1(4) = 0.000000
arrayOfVariantOfDouble1(5) = 0.000000
arrayOfVariantOfDouble1(6) = 0.000000
arrayOfVariantOfDouble1(7) = -0.000000
arrayOfVariantOfDouble1(8) = 1.000000
sketch1.SetAbsoluteAxisData arrayOfVariantOfDouble1
part1.InWorkObject = sketch1
Set factory2D1 = sketch1.OpenEdition()
Set geometricElements1 = sketch1.GeometricElements
Set axis2D1 = geometricElements1.Item("绝对轴")
Set line2D1 = axis2D1.GetItem("横向")
line2D1.ReportName = 1
Set line2D2 = axis2D1.GetItem("纵向")
line2D2.ReportName = 2
Set ellipse2D1 = factory2D1.CreateClosedEllipse(0.000000, 0.000000, 100.000000, 0.000000, 100.000000, 30.000000)
Set point2D1 = axis2D1.GetItem("原点")
ellipse2D1.CenterPoint = point2D1
ellipse2D1.ReportName = 3
Set point2D2 = factory2D1.CreatePoint(0.000000, 30.000000)
point2D2.ReportName = 4
Set constraints1 = sketch1.Constraints
Set reference2 = part1.CreateReferenceFromObject(point2D2)
Set reference3 = part1.CreateReferenceFromObject(line2D2)
Set constraint1 = constraints1.AddBiEltCst(catCstTypeOn, reference2, reference3)
constraint1.Mode = catCstModeDrivingDimension
Set reference4 = part1.CreateReferenceFromObject(ellipse2D1)
Set reference5 = part1.CreateReferenceFromObject(point2D2)
Set constraint2 = constraints1.AddBiEltCst(catCstTypeOn, reference4, reference5)
constraint2.Mode = catCstModeDrivingDimension
sketch1.CloseEdition
part1.InWorkObject = sketch1
part1.UpdateObject sketch1
Set hybridShapeFactory1 = part1.HybridShapeFactory
Set hybridShapePlaneExplicit1 = originElements1.PlaneZX
Set reference6 = part1.CreateReferenceFromObject(hybridShapePlaneExplicit1)
Set hybridShapePlaneOffset1 = hybridShapeFactory1.AddNewPlaneOffset(reference6, 20.000000, False)
body1.InsertHybridShape hybridShapePlaneOffset1
part1.InWorkObject = hybridShapePlaneOffset1
part1.Update
Set hybridShapes1 = body1.HybridShapes
Set reference7 = hybridShapes1.Item("平面.1")
Set sketch2 = sketches1.Add(reference7)
Dim arrayOfVariantOfDouble2(8)
arrayOfVariantOfDouble2(0) = 0.000000
arrayOfVariantOfDouble2(1) = 20.000000
arrayOfVariantOfDouble2(2) = 0.000000
arrayOfVariantOfDouble2(3) = -1.000000
arrayOfVariantOfDouble2(4
4000
) = 0.000000
arrayOfVariantOfDouble2(5) = 0.000000
arrayOfVariantOfDouble2(6) = 0.000000
arrayOfVariantOfDouble2(7) = -0.000000
arrayOfVariantOfDouble2(8) = 1.000000
sketch2.SetAbsoluteAxisData arrayOfVariantOfDouble2
part1.InWorkObject = sketch2
Set factory2D2 = sketch2.OpenEdition()
Set geometricElements2 = sketch2.GeometricElements
Set axis2D2 = geometricElements2.Item("绝对轴")
Set line2D3 = axis2D2.GetItem("横向")
line2D3.ReportName = 1
Set line2D4 = axis2D2.GetItem("纵向")
line2D4.ReportName = 2
Set ellipse2D2 = factory2D2.CreateClosedEllipse(0.000000, 0.000000, 100.000000, 30.000000, 104.403065, 22.044853)
Set point2D3 = axis2D2.GetItem("原点")
ellipse2D2.CenterPoint = point2D3
ellipse2D2.ReportName = 3
sketch2.CloseEdition
part1.InWorkObject = sketch2
part1.UpdateObject sketch2
Set hybridShapeDirection1 = hybridShapeFactory1.AddNewDirectionByCoord(1.000000, 2.000000, 3.000000)
Set reference8 = part1.CreateReferenceFromObject(sketch1)
Set hybridShapeExtremum1 = hybridShapeFactory1.AddNewExtremum(reference8, hybridShapeDirection1, 1)
body1.InsertHybridShape hybridShapeExtremum1
part1.InWorkObject = hybridShapeExtremum1
part1.Update
Set hybridShapeDirection2 = hybridShapeFactory1.AddNewDirectionByCoord(1.000000, 2.000000, 3.000000)
Set reference9 = part1.CreateReferenceFromObject(sketch2)
Set hybridShapeExtremum2 = hybridShapeFactory1.AddNewExtremum(reference9, hybridShapeDirection2, 1)
body1.InsertHybridShape hybridShapeExtremum2
part1.InWorkObject = hybridShapeExtremum2
part1.Update
Set shapeFactory1 = part1.ShapeFactory
Set loft1 = shapeFactory1.AddNewLoft()
Set hybridShapeLoft1 = loft1.HybridShape
hybridShapeLoft1.SectionCoupling = 3
hybridShapeLoft1.Relimitation = 1
hybridShapeLoft1.CanonicalDetection = 2
Set reference10 = part1.CreateReferenceFromObject(sketch1)
hybridShapeLoft1.AddSectionToLoft reference10, 1, Nothing
Set reference11 = part1.CreateReferenceFromObject(sketch2)
hybridShapeLoft1.AddSectionToLoft reference11, 1, Nothing
part1.InWorkObject = hybridShapeLoft1
part1.Update
set reference26=hybridShapes1.Item("多截面实体.1")
Set TheSPAWorkbench=CATIA.ActiveDocument.GetWorkbench("SPAWorkbench")
Set TheMeasurable=TheSPAWorkbench.GetMeasurable(reference26)
AVolume = TheMeasurable.Volume
End Sub




【说明】:

(1)上面的程序通过AVolume变量获得了体积值,如果需要在CATIA中显示出来,则需要借助Msgbox。在Visual Basic编程中,可以传递给相关变量。

(2)如果通过宏程序在Part中生成了实体,但没有“更新”实体,其测量会报错!

(3)如果要对“零件几何体”进行体积测量,以下给出范例程序:

Set objPart=CATIA.ActiveDocument.Part
Set objRef=objPart.CreateReferenceFromObject(objPart.Mainbody)
Set TheSPAWorkbench = CATIA.ActiveDocument.GetWorkbench("SPAWorkbench")
Set TheMeasurable = TheSPAWorkbench.GetMeasurable(objRef)
LoftVolume = TheMeasurable.Volume
Msgbox LoftVolume
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息