1、CATIA V5 Automation Detailed Steps基于VB的catia二次开发代码CATIA V5 AutomationDetailed StepsTable of ContentsViews.vbp 3PartDesign.vbp 5ShapeDesign.vbp 8Assembly.vbp 12Drafting.vbp 15GetPoint.vbp 18TestSelections.vbp 20Views.vbpOption ExplicitDim CATIA As INFITF.ApplicationDim myDoc As PartDocumentPrivate Su
2、b Command1_Click()On Error Resume NextSet CATIA = GetObject(, CATIA.Application)If Err.Number 0 Then Set CATIA = CreateObject(CATIA.Application) CATIA.Visible = TrueEnd IfOn Error GoTo 0 Opening Bolt.CATPartDim myDir As StringmyDir = App.PathSet myDoc = CATIA.Documents.Open(myDir & Bolt.CATPart)Dim
3、myViewer3 As Viewer3DDim myViewPoint As Viewpoint3D Getting the active ViewerSet myViewer3 = CATIA.ActiveWindow.ActiveViewermyViewer3.RenderingMode = catRenderShadingDim myCam3d As Camera3DDim i As Integer Display the number of defined cameras.MsgBox myDoc.Cameras.Count Scaning all the cameras of th
4、e documentFor i = 1 To myDoc.Cameras.Count Set myCam3d = myDoc.Cameras.Item(i) Modifying the Viewpoint3D of the active viewer myViewer3.Viewpoint3D = myCam3d.Viewpoint3D myViewer3.Reframe myViewer3.ZoomIn myViewer3.Update MsgBox myCam3d.NameNext Selecting the front cameraSet myCam3d = myDoc.Cameras.
5、Item(* front)myViewer3.Viewpoint3D = myCam3d.Viewpoint3D Saving the documentIf MsgBox(Save Bolt2, vbOKCancel) = vbOK Then On Error Resume Next Kill (myDir & Bolt2.CATPart) Call myDoc.SaveAs(myDir & Bolt2.CATPart) Call myDoc.Close On Error GoTo 0End IfEnd SubPartDesign.vbpOption ExplicitDim CATIA As
6、INFITF.ApplicationPrivate Sub Command1_Click() On Error Resume NextSet CATIA = GetObject(, CATIA.Application)If Err.Number 0 Then Set CATIA = CreateObject(CATIA.Application) CATIA.Visible = TrueEnd IfOn Error GoTo 0 Creating a new PartDim MyDoc As PartDocumentSet MyDoc = CATIA.Documents.Add(Part) Ge
7、tting the default Body called MechanicalTool.1 (internal name)Dim myBody As BodySet myBody = MyDoc.Part.Bodies.Item(MechanicalTool.1) Activating the body as the InWorkObjectMyDoc.Part.InWorkObject = myBody Creating a reference on XY planeDim ReferencePlane As ReferenceSet ReferencePlane = MyDoc.Part
8、.CreateReferenceFromGeometry(MyDoc.Part.OriginElements.PlaneXY) Creating mySketch1 on XY PlaneDim mySketch1 As SketchSet mySketch1 = myBody.Sketches.Add(ReferencePlane) Opening mySketch1 and getting the factoryDim MyFactory1 As Factory2DSet MyFactory1 = mySketch1.OpenEdition() Creating 4 linesDim l1
9、 As Line2DDim l2 As Line2DDim l3 As Line2DDim l4 As Line2DSet l1 = MyFactory1.CreateLine(10#, 10#, 10#, 30#)Set l2 = MyFactory1.CreateLine(10#, 30#, 40#, 30#)Set l3 = MyFactory1.CreateLine(40#, 30#, 40#, 10#)Set l4 = MyFactory1.CreateLine(40#, 10#, 10#, 10#)mySketch1.CloseEdition Creating mySketch2
10、on XY PlaneDim mySketch2 As SketchSet mySketch2 = myBody.Sketches.Add(ReferencePlane) Opening mySketch2 and getting the factoryDim MyFactory2 As Factory2DSet MyFactory2 = mySketch2.OpenEdition() Creating 1 CircleDim c1 As Circle2DSet c1 = MyFactory2.CreateClosedCircle(40#, 30#, 10#)mySketch2.CloseEd
11、ition Getting the shapeFactoryDim MyFact As ShapeFactorySet MyFact = MyDoc.Part.ShapeFactory Creating a padDim myPad As PadSet myPad = MyFact.AddNewPad(mySketch1, 20) Creating a PocketDim myPok As PocketSet myPok = MyFact.AddNewPocket(mySketch2, -20)MyDoc.Part.Update Reframing the window on the part
12、CATIA.ActiveWindow.ActiveViewer.ReframeEnd SubShapeDesign.vbpOption ExplicitDim CATIA As INFITF.ApplicationPrivate Sub Command1_Click() On Error Resume NextSet CATIA = GetObject(, CATIA.Application)If Err.Number 0 Then Set CATIA = CreateObject(CATIA.Application) CATIA.Visible = TrueEnd IfOn Error Go
13、To 0Dim myPartDocument As PartDocumentSet myPartDocument = CATIA.Documents.Add(Part)Dim myPart As PartSet myPart = myPartDocument.PartCreating an Open_body if not already existingOn Error Resume NextDim myHybridBody As HybridBodySet myHybridBody = myPart.HybridBodies.Item(Open_body.1)If myHybridBody
14、 Is Nothing Then Set myHybridBody = myPart.HybridBodies.AddEnd IfOn Error GoTo 0Dim MyHSFact As HybridShapeFactorySet MyHSFact = myPart.HybridShapeFactorymyPart.Update Creating 6 PointsDim HyPt1 As HybridShapePointCoordDim HyPt2 As HybridShapePointCoordDim HyPt3 As HybridShapePointCoordDim HyPt4 As
15、HybridShapePointCoordDim HyPt5 As HybridShapePointCoordDim HyPt6 As HybridShapePointCoordSet HyPt1 = MyHSFact.AddNewPointCoord(10#, 60#, 30#)Set HyPt2 = MyHSFact.AddNewPointCoord(70#, 75#, 35#)Set HyPt3 = MyHSFact.AddNewPointCoord(100#, 80#, 30#)Set HyPt4 = MyHSFact.AddNewPointCoord(100#, 80#, 40#)S
16、et HyPt5 = MyHSFact.AddNewPointCoord(95#, 20#, 45#)Set HyPt6 = MyHSFact.AddNewPointCoord(100#, 10#, 50#) Creating references on the pointsDim R1 As ReferenceDim R2 As ReferenceDim R3 As ReferenceDim R4 As ReferenceDim R5 As ReferenceDim R6 As ReferenceSet R1 = myPart.CreateReferenceFromGeometry(HyPt
17、1)Set R2 = myPart.CreateReferenceFromGeometry(HyPt2)Set R3 = myPart.CreateReferenceFromGeometry(HyPt3)Set R4 = myPart.CreateReferenceFromGeometry(HyPt4)Set R5 = myPart.CreateReferenceFromGeometry(HyPt5)Set R6 = myPart.CreateReferenceFromGeometry(HyPt6)Dim myControlPoint As HybridShapeControlPoint We
18、 can reuse the same variable: myControlPoint Creating first SplineDim HyLine1 As HybridShapeSplineSet HyLine1 = MyHSFact.AddNewSplineHyLine1.SetSplineType 0HyLine1.SetClosing 0 Adding the control pointsSet myControlPoint = MyHSFact.AddNewControlPoint(R1)HyLine1.AddControlPoint myControlPointSet myCo
19、ntrolPoint = MyHSFact.AddNewControlPoint(R2)HyLine1.AddControlPoint myControlPointSet myControlPoint = MyHSFact.AddNewControlPoint(R3)HyLine1.AddControlPoint myControlPoint Creating second SplineDim HyLine2 As HybridShapeSplineSet HyLine2 = MyHSFact.AddNewSplineCall HyLine2.SetSplineType(0)Call HyLi
20、ne2.SetClosing(0)Set myControlPoint = MyHSFact.AddNewControlPoint(R4)HyLine2.AddControlPoint myControlPointSet myControlPoint = MyHSFact.AddNewControlPoint(R5)HyLine2.AddControlPoint myControlPointSet myControlPoint = MyHSFact.AddNewControlPoint(R6)HyLine2.AddControlPoint myControlPointDim Ref1 As R
21、eferenceDim Ref2 As Reference Creating a SweepSet Ref1 = myPart.CreateReferenceFromGeometry(HyLine1)Set Ref2 = myPart.CreateReferenceFromGeometry(HyLine2)Dim HybridShapeSweepExplicit1 As HybridShapeSweepExplicitSet HybridShapeSweepExplicit1 = MyHSFact.AddNewSweepExplicit(Ref1, Ref2)myHybridBody.Appe
22、ndHybridShape HyLine1myHybridBody.AppendHybridShape HyLine2myHybridBody.AppendHybridShape HybridShapeSweepExplicit1 Creating a 3D PointDim HyPt7 As HybridShapePointCoordSet HyPt7 = MyHSFact.AddNewPointCoord(50#, 30#, 100#)myHybridBody.AppendHybridShape HyPt7 Creating the projection of HyPt7 on the s
23、urface HybridShapeSweepExplicit1Dim Ref3 As ReferenceDim Ref4 As ReferenceSet Ref3 = myPart.CreateReferenceFromGeometry(HyPt7)Set Ref4 = myPart.CreateReferenceFromGeometry(HybridShapeSweepExplicit1)Dim HybridShapeProject1 As HybridShapeProjectSet HybridShapeProject1 = MyHSFact.AddNewProject(Ref3, Re
24、f4)myHybridBody.AppendHybridShape HybridShapeProject1myPart.UpdateEnd SubAssembly.vbpOption ExplicitDim CATIA As ObjectPrivate Sub Command1_Click()Dim i As IntegerReDim myNames(1) As StringOn Error Resume NextSet CATIA = GetObject(, CATIA.Application)If Err.Number 0 Then Set CATIA = CreateObject(CAT
25、IA.Application) CATIA.Visible = TrueEnd IfOn Error GoTo 0Dim myDir As StringmyDir = App.Path Creating the root productDim Titanic As ProductDocumentSet Titanic = CATIA.Documents.Add(Product) Creating the hull at level 1myNames(0) = myDir + Hull.CATPartCall Titanic.Product.Products.AddComponentsFromF
26、iles(myNames, *)Titanic.Product.PartNumber = TitanicDim assy1 As Product Creating sub-product ass1 at level 1New Product not associated with a ProductDocument fileSet assy1 = Titanic.Product.Products.AddNewProduct(ass1) Creating the Castle under ass1 at level 2myNames(0) = myDir + Castle.CATPartCall
27、 assy1.Products.AddComponentsFromFiles(myNames, *) Creating the Funnel under ass1 at level 2myNames(0) = myDir + Funnel.CATPartCall assy1.Products.AddComponentsFromFiles(myNames, *)Titanic.Product.UpdateDim Ass1RefProduct As ProductSet Ass1RefProduct = assy1.ReferenceProduct Creating 2nd instance of
28、 subproduct ass1 at level 1Dim Product2 As ProductSet Product2 = Titanic.Product.Products.AddComponent(Ass1RefProduct)Creating the transformation matrixReDim var1(11)var1(0) = 1#var1(1) = 0#var1(2) = 0#var1(3) = 0#var1(4) = 1#var1(5) = 0#var1(6) = 0#var1(7) = 0#var1(8) = 1#var1(9) = 60# translation
29、along Xvar1(10) = 0# translation along Yvar1(11) = 0# translation along Z Moving the instanceProduct2.Move.Apply var1 Creating 3rd instance of ass1 at level 1Dim Product3 As ProductSet Product3 = Titanic.Product.Products.AddComponent(Ass1RefProduct)var1(9) = 120# translation along X Moving the insta
30、nceProduct3.Move.Apply var1 Creating the Bill Of Material in the file TitanicBOM.txtCall Titanic.Product.ExtractBOM(catFileTypeText, myDir & TitanicBOM.txt)End SubDrafting.vbpOption ExplicitDim CATIA As INFITF.ApplicationPrivate Sub Command1_Click()Dim i As IntegerOn Error Resume NextSet CATIA = GetObject(, CATIA.Application)If Err.Number 0 Then Set CATIA = CreateObject(CATIA.Application) CATIA.Visible = TrueEnd IfDim myDir As StringmyDir = App.Path Opening Bolt.CATPart fileDim MyPartDoc As PartDocumentSet MyPartDoc =
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1