Inventor 设计软件在我们中国使用的不是很广,其中VBA方面的书籍很难找到,朋友们不要保守,跟帖说说你们的心得,有好主意拿出来大家分享共同进步!谢谢大家的关注!!!
对Inventor VBA不是很熟的朋友 想要看VBA帮助的话 我想都会和我当初一样找不到范例 按下F1 帮助键 只看到 Visual Basic 参考 !!如何看 如何学习??? 大家可能都会觉得没有头绪??
后面我会告诉大家如何看VBA帮助
'读取现有图纸数量
Public Sub countDrawingSheets()
Dim oDrawDoc As DrawingDocument
If ThisApplication.ActiveDocument.DocumentType = kDrawingDocumentObject Then
Set oDrawDoc = ThisApplication.ActiveDocument
Debug.Print "Number of Sheets = " & oDrawDoc.Sheets.Count
End If
End Sub
'删除多余的图框
Public Sub getBorderNames()
Dim oDrawDoc As DrawingDocument
'Ensure the active document is a drawing
If ThisApplication.ActiveDocument.DocumentType = kDrawingDocumentObject Then
Set oDrawDoc = ThisApplication.ActiveDocument
Dim oBorders As BorderDefinitions
Dim oBorderDef As BorderDefinition
Set oBorders = oDrawDoc.BorderDefinitions
'loop through the border collection and print out then
' name of each border
For Each oBorderDef In oBorders
Debug.Print oBorderDef.Name
On Error Resume Next
oBorderDef.Delete
Next oBorderDef
End If
End Sub
'增加标题栏
Public Sub addMyTitleBlock()
Dim oDrawDoc As DrawingDocument
'Ensure the active document is a drawing
If ThisApplication.ActiveDocument.DocumentType = kDrawingDocumentObject Then
Set oDrawDoc = ThisApplication.ActiveDocument
' get the Title blocks (definitions)
Dim oTitleBlks As TitleBlockDefinitions
Set oTitleBlks = oDrawDoc.TitleBlockDefinitions
' add a new title block definition
Dim oTitleBlk As TitleBlockDefinition
Set oTitleBlk = oTitleBlks.Add("myxTitleBlock")
End If
End Sub
'增加图框
Public Sub Demo23_CreateBorderDefinition()
Dim oDrawDoc As DrawingDocument
Set oDrawDoc = ThisApplication.ActiveDocument
' Create the new border definition.
Dim oBorderDef As BorderDefinition
Set oBorderDef = oDrawDoc.BorderDefinitions.Add("My Border")
' Open the border definition's sketch for edit. This is done by calling the Edit
' method of the BorderDefinition to obtain a DrawingSketch. This actually creates
' a copy of the border definition's and opens it for edit.
Dim oSketch As DrawingSketch
Call oBorderDef.Edit(oSketch)
Dim oTG As TransientGeometry
Set oTG = ThisApplication.TransientGeometry
' Use the functionality of the sketch to add geometry.
Call oSketch.SketchLines.AddAsTwoPointRectangle(oTG.CreatePoint2d(2, 2), _
oTG.CreatePoint2d(25.94, 19.59))
Call oBorderDef.ExitEdit(True)
End Sub
'增加缩略符号
Public Sub Demo23_CreateSketchedSymbolDefinition()
' Set a reference to the drawing document.
If ThisApplication.ActiveDocument.DocumentType <> kDrawingDocumentObject Then Exit Sub
Dim oDrawDoc As DrawingDocument
Set oDrawDoc = ThisApplication.ActiveDocument
' Create the new sketched symbol definition.
Dim oSketchedSymbolDef As SketchedSymbolDefinition
Set oSketchedSymbolDef = oDrawDoc.SketchedSymbolDefinitions.Add("Circular Callout")
' Open the sketched symbol definition's sketch for edit. This is done by calling the Edit
' method of the SketchedSymbolDefinition to obtain a DrawingSketch. This actually creates
' a copy of the sketched symbol definition's and opens it for edit.
Dim oSketch As DrawingSketch
Call oSketchedSymbolDef.Edit(oSketch)
Dim oTG As TransientGeometry
Set oTG = ThisApplication.TransientGeometry
' Use the functionality of the sketch to add sketched symbol graphics.
Dim oSketchLine As SketchLine
Set oSketchLine = oSketch.SketchLines.AddByTwoPoints(oTG.CreatePoint2d(0, 0), oTG.CreatePoint2d(20, 0))
Dim oSketchCircle As SketchCircle
Set oSketchCircle = oSketch.SketchCircles.AddByCenterRadius(oTG.CreatePoint2d(22, 0), 2)
Call oSketch.GeometricConstraints.AddCoincident(oSketchLine.EndSketchPoint, oSketchCircle)
' Make the starting point of the sketch line the insertion point
oSketchLine.StartSketchPoint.InsertionPoint = True
' Add a prompted text field at the center of the sketch circle.
Dim sText As String
sText = "<StyleOverride FontSize='1'>< rompt>Enter text 1</Prompt></StyleOverride>"
Dim oTextBox As TextBox
Set oTextBox = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(22, 0), sText)
oTextBox.VerticalJustification = kAlignTextMiddle
oTextBox.HorizontalJustification = kAlignTextCenter
Call oSketchedSymbolDef.ExitEdit(True)
End Sub
'增加已经有的缩略符号的基础上加上字符标志以及角度等信息
Public Sub Demo23_InsertSketchedSymbolOnSheet()
If ThisApplication.ActiveDocument.DocumentType <> kDrawingDocumentObject Then Exit Sub
' Set a reference to the drawing document.
Dim oDrawDoc As DrawingDocument
Set oDrawDoc = ThisApplication.ActiveDocument
' Obtain a reference to the desired sketched symbol definition.
Dim oSketchedSymbolDef As SketchedSymbolDefinition
Set oSketchedSymbolDef = oDrawDoc.SketchedSymbolDefinitions.Item("Circular Callout")
Dim oSheet As Sheet
Set oSheet = oDrawDoc.ActiveSheet
' This sketched symbol definition contains one prompted string input. An array
' must be input that contains the strings for the prompted strings.
Dim sPromptStrings(0) As String
sPromptStrings(0) = "A"
Dim oTG As TransientGeometry
Set oTG = ThisApplication.TransientGeometry
' Add an instance of the sketched symbol definition to the sheet.
' Rotate the instance by 45 degrees and scale by .75 when adding.
' The symbol will be inserted at (0,0) on the sheet. Since the
' start point of the line was marked as the insertion point, the
' start point should end up at (0,0).
Dim oSketchedSymbol As SketchedSymbol
Set oSketchedSymbol = oSheet.SketchedSymbols.Add(oSketchedSymbolDef, _
oTG.CreatePoint2d(5, 5), _
(3.14159 / 4), _
0.75, sPromptStrings)
End Sub
'增加图框
Public Sub InsertCustomBorderOnSheet()
' Set a reference to the drawing document.
' This assumes a drawing document is active.
Dim oDrawDoc As DrawingDocument
Set oDrawDoc = ThisApplication.ActiveDocument
' Obtain a reference to the desired border definition.
Dim oBorderDef As BorderDefinition
Set oBorderDef = oDrawDoc.BorderDefinitions.Item("11 x 17 Border")
MsgBox oBorderDef.Name
Dim oSheet As Sheet
Set oSheet = oDrawDoc.ActiveSheet
' Check to see if the sheet already has a border and delete it if it does.
If Not oSheet.Border Is Nothing Then
oSheet.Border.Delete
End If
' This border definition contains one prompted string input. An array
' must be input that contains the strings for the prompted strings.
Dim sPromptStrings(1 To 1) As String
sPromptStrings(1) = "This is the input for the prompted text."
' Add an instance of the border definition to the sheet.
Dim oBorder As Border
Set oBorder = oSheet.AddBorder(oBorderDef, sPromptStrings)
End Sub
'增加一张新的图纸并且放入指定的标题栏
Public Sub Demo23_addSheetAddBorder()
If ThisApplication.ActiveDocument.DocumentType <> kDrawingDocumentObject Then Exit Sub
' Get the Drawing document
Dim oDrawDoc As DrawingDocument
Set oDrawDoc = ThisApplication.ActiveDocument
' Get the Sheets collection
Dim oSheets As Sheets
Set oSheets = oDrawDoc.Sheets
Dim oSheet As Sheet
For Each oSheet In oSheets
Debug.Print oSheet.Name
Next oSheet
' Add a new sheet with a default border and a Title block
Set oSheet = oSheets.Add(kA3DrawingSheetSize, kDefaultPageOrientation, "Sheet")
Call oSheet.AddBorder(oDrawDoc.BorderDefinitions.Item("11 x 17 Border"))
Call oSheet.AddTitleBlock(oDrawDoc.TitleBlockDefinitions("11 x 17 Titleblock "))
End Sub
'出图 普通出图
Public Sub Demo23_AddViews()
' Open the model to create a view
Dim oPartDoc As PartDocument
Set oPartDoc = ThisApplication.Documents.Open("C:Temp.ipt", False)
If ThisApplication.ActiveDocument.DocumentType <> kDrawingDocumentObject Then Exit Sub
Dim oDrawDoc As DrawingDocument
Set oDrawDoc = ThisApplication.ActiveDocument
Dim oSheet As Sheet
Set oSheet = oDrawDoc.ActiveSheet
Dim oTG As TransientGeometry
Set oTG = ThisApplication.TransientGeometry
' Create the base view.
Dim oFrontView As DrawingView
Set oFrontView = oSheet.DrawingViews.AddBaseView(oPartDoc, oTG.CreatePoint2d(15, 5), 1 / 8, _
kFrontViewOrientation, kHiddenLineDrawingViewStyle)
' close the part file
oPartDoc.Close
' Create projected views.
Dim oRightView As DrawingView
Set oRightView = oSheet.DrawingViews.AddProjectedView(oFrontView, oTG.CreatePoint2d(15, 15), kFromBaseDrawingViewStyle)
Dim oLeftView As DrawingView
Set oLeftView = oSheet.DrawingViews.AddProjectedView(oFrontView, oTG.CreatePoint2d(30, 5), kFromBaseDrawingViewStyle)
Dim oIsoView As DrawingView
Set oIsoView = oSheet.DrawingViews.AddProjectedView(oFrontView, oTG.CreatePoint2d(27, 21), kHiddenLineRemovedDrawingViewStyle)
End Sub
'不知道干嘛用的
Sub drawCurvesIterate()
If ThisApplication.ActiveDocument.DocumentType <> kDrawingDocumentObject Then Exit Sub
Dim oDrawDoc As DrawingDocument
Dim i As Integer
Dim oSheet As Sheet
Dim oDrawView As DrawingView
Set oDrawDoc = ThisApplication.ActiveDocument
Set oSheet = oDrawDoc.ActiveSheet
For Each oDrawView In oSheet.DrawingViews
Dim oDrawViewCurves As DrawingCurvesEnumerator
Set oDrawViewCurves = oDrawView.DrawingCurves()
Debug.Print "Number of Entities in " & oDrawView.Name & " " & oDrawViewCurves.Count
Dim oDrawCurve As DrawingCurve
For i = 1 To oDrawViewCurves.Count
Set oDrawCurve = oDrawViewCurves.Item(i)
Select Case oDrawCurve.CurveType
Case kUnknownCurve:
Debug.Print "Unknown curve type."
Case kLineCurve:
Debug.Print "Line curve."
Case kLineSegmentCurve
Debug.Print "Line segment curve."
Case kCircleCurve
Debug.Print "Circular curve."
Case kCircularArcCurve
Debug.Print "Circular arc curve."
Case kEllipseFullCurve
Debug.Print "Ellipse full curve."
Case kEllipticalArcCurve
Debug.Print "Elliptical arc curve."
Case kBSplineCurve
Debug.Print "B-spline curve."
End Select
Next
Next
'Dim oDrawCurve As DrawingCurve
'Set oDrawCurve = oDrawCurvesEnumerator(1)
' Iterate through the curves in the enumerator
'For Each oDrawCurve In oDrawCurvesEnumerator
' Get the Geometry intent for the drawing curve
'Dim oGeometryIntent As GeometryIntent
'Set oGeometryIntent = oSheet.CreateGeometryIntent(oDrawCurve)
'Print out the IntentType
'Debug.Print oGeometryIntent.IntentType
'Next oDrawCurve
End Sub
'创建局部放大图
Public Sub CreateDetailView()
Dim oDrawDoc As DrawingDocument
Set oDrawDoc = ThisApplication.ActiveDocument
Dim oSheet As Sheet
Set oSheet = oDrawDoc.ActiveSheet
' Check to make sure a drawing view is selected.
If Not TypeOf oDrawDoc.SelectSet.Item(1) Is DrawingView Then
MsgBox "A drawing view must be selected."
Exit Sub
End If
' Set a reference to the selected drawing. This assumes
' that the selected view is not a draft view.
Dim oDrawingView As DrawingView
Set oDrawingView = oDrawDoc.SelectSet.Item(1)
' Set a reference to the center of the base view.
Dim oPoint As Point2d
Set oPoint = oDrawingView.Center
' Translate point by a distance = 2 * width of the view
' This will be the placement point of the detail view.
oPoint.X = oPoint.X + 2 * oDrawingView.Width
' Set corner one of rectangular fence as
' the left‐bottom corner of the base view.
Dim oCornerOne As Point2d
Set oCornerOne = oDrawingView.Center
oCornerOne.X = oCornerOne.X - oDrawingView.Width / 2
oCornerOne.Y = oCornerOne.Y - oDrawingView.Height / 2
' Set corner two of rectangular fence as
' the center of the base view.
Dim oCornerTwo As Point2d
Set oCornerTwo = oDrawingView.Center
' Get any linear curve from the base view
Dim oCurve As DrawingCurve ' DrawingViewCurve
For Each oCurve In oDrawingView.DrawingCurves ' .DrawingViewCurves
Debug.Print oCurve.CurveType
If oCurve.CurveType = kLineSegmentCurve Then
Exit For
End If
Next
' Create an intent object
Dim oAttachPoint As GeometryIntent
Set oAttachPoint = oSheet.CreateGeometryIntent(oCurve, kStartPointIntent)
' Create the detail view
Dim oDetailView As DetailDrawingView
Set oDetailView = oSheet.DrawingViews.AddDetailView(oDrawingView, oPoint, _
kFromBaseDrawingViewStyle, False, oCornerOne, oCornerTwo, oAttachPoint, 2)
End Sub
'增加自定义标注
Public Sub addMyDimension()
If ThisApplication.ActiveDocument.DocumentType <> kDrawingDocumentObject Then Exit Sub
Dim oDrawDoc As DrawingDocument
Set oDrawDoc = ThisApplication.ActiveDocument
Dim oSheet As Sheet
Set oSheet = oDrawDoc.ActiveSheet
Dim oSelectSet As SelectSet
Set oSelectSet = oDrawDoc.SelectSet
Dim oTG As TransientGeometry
Set oTG = ThisApplication.TransientGeometry
' Test to see if one entity is selected
If oSelectSet.Count = 1 Then
'Test to see if the selected entity is a sketch line
Dim oObj As Object
Set oObj = oSelectSet.Item(1)
If TypeOf oSelectSet.Item(1) Is DrawingCurveSegment Then
Dim oDrawCurveSegment As DrawingCurveSegment
Set oDrawCurveSegment = oSelectSet(1)
Dim oDrawingCurve As DrawingCurve
Set oDrawingCurve = oDrawCurveSegment.Parent
Dim oGeomIntent1 As GeometryIntent
Dim oGeomIntent2 As GeometryIntent
Set oGeomIntent1 = oSheet.CreateGeometryIntent(oDrawingCurve, kStartPointIntent)
Set oGeomIntent2 = oSheet.CreateGeometryIntent(oDrawingCurve, kEndPointIntent)
' Add a linear dimension
Dim oGenDims As GeneralDimensions
Set oGenDims = oSheet.DrawingDimensions.GeneralDimensions
Dim oLinDim As LinearGeneralDimension
Set oLinDim = oGenDims.AddLinear(oTG.CreatePoint2d(10, 10), oGeomIntent1, oGeomIntent2)
End If
End If
End Sub
'增加工程图资源中的自定义图框
Public Sub CreateBorderDefinition()
' Set a reference to the drawing document.
' This assumes a drawing document is active.
Dim oDrawDoc As DrawingDocument
Set oDrawDoc = ThisApplication.ActiveDocument
' Create the new borderdefinition.
Dim oBorderDef As BorderDefinition
Set oBorderDef = oDrawDoc.BorderDefinitions.Add("Sample Border")
' Open the border definition's sketch for edit. This is done by calling the Edit
' method of the BorderDefinition to obtain a DrawingSketch. This actually creates
' a copy of the border definition's and opens it for edit.
Dim oSketch As DrawingSketch
Call oBorderDef.Edit(oSketch)
Dim oTG As TransientGeometry
Set oTG = ThisApplication.TransientGeometry
' Use the functionality of the sketch to add geometry.
Call oSketch.SketchLines.AddAsTwoPointRectangle(oTG.CreatePoint2d(2, 2), oTG.CreatePoint2d(25.94, 19.59))
Call oSketch.SketchLines.AddByTwoPoints(oTG.CreatePoint2d(0, 10.795), oTG.CreatePoint2d(2, 10.795))
Call oSketch.SketchLines.AddByTwoPoints(oTG.CreatePoint2d(13.97, 0), oTG.CreatePoint2d(13.97, 2))
Call oSketch.SketchLines.AddByTwoPoints(oTG.CreatePoint2d(25.94, 10.795), oTG.CreatePoint2d(27.94, 10.795))
Call oSketch.SketchLines.AddByTwoPoints(oTG.CreatePoint2d(13.97, 19.59), oTG.CreatePoint2d(13.97, 21.59))
' Add some text to the border.
Dim oTextBox As TextBox
Set oTextBox = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(2, 1), "Here is a sample string")
oTextBox.VerticalJustification = kAlignTextMiddle
' Add some prompted text to the border.
Set oTextBox = oSketch.TextBoxes.AddFitted(oTG.CreatePoint2d(2, 20.59), "Enter designers name:")
oTextBox.VerticalJustification = kAlignTextMiddle
Call oBorderDef.ExitEdit(True)
End Sub |
|