草图中有多种图元,例如圆,线等。有时新的草图我们需要重用旧草图的图元,不需要再画一遍,产品里可以采取复制粘贴。API提供了Sketch.CopyContentsTo,可以从一个草图把所有内容复制到另外草图。如果是不同零件中的草图,只需要注意在复制后调用一下文档的Update。以下是个简单的代码,它把零件1的草图1图元内容复制到零件2的草图1.
Sub VBAMain()
Dim oPart1 As PartDocument
Set oPart1 = ThisApplication.Documents.Open("c:\part1.ipt", False)
Dim oSketch_inPart1 As Sketch
Set oSketch_inPart1 = oPart1.ComponentDefinition.Sketches(1)
Dim oPart2 As PartDocument
Set oPart2 = ThisApplication.Documents.Open("c:\part2.ipt", False)
Dim oSketch_inPart2 As Sketch
Set oSketch_inPart2 = oPart2.ComponentDefinition.Sketches(1)
Call oSketch_inPart1.CopyContentsTo(oSketch_inPart2)
Call oPart1.Close(True)
'记住 update!
oPart2.Update
oPart2.Save
Call oPart2.Close(True)
End Sub