机械必威体育网址
标题:
solidworks自己录制的VBA代码有问题
[打印本页]
作者:
jinjunbai
时间:
2019-6-8 14:12
标题:
solidworks自己录制的VBA代码有问题
本帖最后由 jinjunbai 于 2019-6-8 14:17 编辑
/ g6 }3 I1 H1 j- O2 i
' w8 ^ q* I- I. ]! Q; D6 A' M
今天尝试用VBA代码完成一个图形的绘制,发现程序自己录制的VBA执行都有问题,比如基准面,绘图的时候设置好,VBA中执行出来就没有了,请高手帮忙解决一下
2 K5 t6 i) l: c/ X
6 v. c6 e+ q) @3 y2 @. G# x. M8 k
代码如下:
% {2 e9 ?: K7 H4 o* p
' ******************************************************************************
8 Y5 z9 V. i: n4 Y' h
' C:\Users\admin\AppData\Local\Temp\swx11724\Macro1.swb - macro recorded on 06/08/19 by admin
3 t4 H& M0 J9 X5 w
' ******************************************************************************
% p: V4 @! ~3 w- f4 l7 U
Dim swApp As Object
' z1 s5 O9 P0 a" Q: N
! W+ F. ]6 |5 i4 B
Dim Part As Object
/ d4 W. ]6 k# t* m& O- E& ?
Dim boolstatus As Boolean
/ {4 }9 H- C7 b! l+ H
Dim longstatus As Long, longwarnings As Long
% e' ?* T- d y( ^# U
* h; `% c' Y, H; }
Sub main()
5 N8 m3 A% E z/ o/ M D
8 x" L: C- w- f; N: U2 Y$ \
Set swApp = Application.SldWorks
, L/ f# I( T$ W# T2 F
& `: d% r( {1 _8 G* l0 T
$ u3 [$ q' u% Y* ?# k4 x: @
' New Document
' Q `$ Y& a8 ~. Q
Dim swSheetWidth As Double
$ {$ s/ e( s! Q
swSheetWidth = 0
& w. e. u% x; O/ d2 Q
Dim swSheetHeight As Double
9 L7 W( I* O% t, Y/ B2 A% D( o- C8 I
swSheetHeight = 0
3 h8 |" D } O Q
Set Part = swApp.NewDocument("C:\ProgramData\SolidWorks\SOLIDWORKS 2018\templates\gb_part.prtdot", 0, swSheetWidth, swSheetHeight)
+ w7 A) _! _; N& F6 D5 ^: B% P" H
Dim swPart As PartDoc
9 Y. R) K5 i% `) P) M
Set swPart = Part
2 Y* Y. f5 d/ }3 I j
swApp.ActivateDoc2 "零件1", False, longstatus
6 ?5 l' E1 ~: g7 s* b2 U
Set Part = swApp.ActiveDoc
( g& r6 U/ F- B' w& d, E+ h6 l
Dim myModelView As Object
+ ]7 O4 A6 p# P* K1 B
Set myModelView = Part.ActiveView
5 K+ a6 z# T N& Y6 C
myModelView.FrameState = swWindowState_e.swWindowMaximized
: g3 q/ G% S. L' r( k" y5 S* D8 P" g
boolstatus = Part.Extension.SelectByID2("注解", "DCABINET", 0, 0, 0, False, 0, Nothing, 0)
0 e- Y8 \! C9 S# ^
boolstatus = Part.Extension.SelectByID2("前视基准面", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
' B; Q% d, M2 h* x# i+ E
Part.SketchManager.InsertSketch True
4 e" t; i% d: O0 W: H
Part.ClearSelection2 True
" {: F/ x' Z* a4 m3 E
boolstatus = Part.Extension.SetUserPreferenceToggle(swUserPreferenceToggle_e.swSketchAddConstToRectEntity, swUserPreferenceOption_e.swDetailingNoOptionSpecified, False)
; S9 [. z1 z- m, n" C' |2 ^
boolstatus = Part.Extension.SetUserPreferenceToggle(swUserPreferenceToggle_e.swSketchAddConstLineDiagonalType, swUserPreferenceOption_e.swDetailingNoOptionSpecified, True)
% b: ]4 n$ L7 n6 i- y
Dim vSkLines As Variant
. U+ J3 |4 }5 E
vSkLines = Part.SketchManager.CreateCornerRectangle(-4.03305583756345E-02, 3.97460575296108E-02, 0, 6.89710998307952E-02, -0.03010179357022, 0)
9 x) E& v& c1 r d" N) ~' |" v
6 P7 t0 a; b1 Z$ y" a6 Q
' Named View
/ B0 r& k. l$ j
Part.ShowNamedView2 "*上下二等角轴测", 8
{+ [. T/ Y& c2 d. m; L
Part.ViewZoomtofit2
" b, N% K) n. H8 @
Dim myFeature As Object
6 i2 {' X9 S8 t7 d M O
Set myFeature = Part.FeatureManager.FeatureExtrusion2(True, False, False, 0, 0, 0.01, 0.01, False, False, False, False, 1.74532925199433E-02, 1.74532925199433E-02, False, False, False, False, True, True, True, 0, 0, False)
; j3 Z2 N5 b: H/ N" e6 a: g& l- w+ Y/ g
Part.SelectionManager.EnableContourSelection = False
& d5 V. c4 s& W* \
boolstatus = Part.Extension.SelectByRay(-1.52826298517539E-02, 1.47929888240128E-02, 9.99999999999091E-03, -0.400036026779312, -0.515038074910024, -0.758094294050284, 5.70826886238244E-04, 2, False, 0, 0)
2 p7 x) `7 S8 u
Part.ClearSelection2 True
3 e h( Y( b: ~( Q
boolstatus = Part.Extension.SelectByRay(-1.52826298517539E-02, 1.47929888240128E-02, 9.99999999999091E-03, -0.400036026779312, -0.515038074910024, -0.758094294050284, 5.70826886238244E-04, 2, False, 0, 0)
" z6 a6 T) l }' w4 D( B5 a
Part.ClearSelection2 True
3 H9 k: i8 ?8 L1 t$ J3 E
boolstatus = Part.Extension.SelectByID2("前视基准面", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
4 f# W" w* n# W0 _
boolstatus = Part.Extension.SelectByID2("前视基准面", "PLANE", 0, 0, 0, True, 0, Nothing, 0)
* v5 T* ?! y) D" D1 C
Dim myRefPlane As Object
5 y3 `, S9 `; K
Set myRefPlane = Part.FeatureManager.InsertRefPlane(8, 0.01, 0, 0, 0, 0)
. n; n. k0 M( Z. Z1 w! p7 B
Part.ClearSelection2 True
6 @% U4 I0 t9 I8 D# ]7 ]% n
boolstatus = Part.Extension.SelectByID2("前视基准面", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
7 d3 E/ G; p" ^3 _
Part.ClearSelection2 True
8 ]: ^! b) Z7 w3 ^1 Q: P) j
Part.ClearSelection2 True
8 ^& q' e+ W }; R' C
boolstatus = Part.Extension.SetUserPreferenceToggle(swUserPreferenceToggle_e.swSketchAddConstToRectEntity, swUserPreferenceOption_e.swDetailingNoOptionSpecified, False)
7 g! H8 `4 @; n( e, H; I9 j
boolstatus = Part.Extension.SetUserPreferenceToggle(swUserPreferenceToggle_e.swSketchAddConstLineDiagonalType, swUserPreferenceOption_e.swDetailingNoOptionSpecified, True)
" F* c" J: v3 t$ p
vSkLines = Part.SketchManager.CreateCornerRectangle(-1.26249913529932E-02, 1.98473013094258E-02, 0, 4.43244050501335E-02, -1.64793375533918E-02, 0)
: h% e k, F( |5 c
Set myFeature = Part.FeatureManager.FeatureExtrusion2(True, False, False, 0, 0, 0.01, 0.01, False, False, False, False, 1.74532925199433E-02, 1.74532925199433E-02, False, False, False, False, True, True, True, 0, 0, False)
$ y/ Y7 L% M* ]' G; w# s7 O
Part.SelectionManager.EnableContourSelection = False
$ r2 D! ]. ~" }1 m
End Sub
$ D' F* D+ K! y3 c9 A
/ {, {5 {9 W: C1 \" ^* M
# z7 o F; E2 [* d. r# i0 M( @
作者:
gddx110
时间:
2019-6-8 16:00
SW录制的部分动作不会记录,需要对二次开发的语句有一定了解才能修改,建议看一下API帮助文档入门后再提问。
作者:
魍者归来
时间:
2019-6-8 16:29
先说清楚自己想实现什么动作
0 [7 l' x/ |( Y4 y
作者:
jinjunbai
时间:
2019-6-8 16:49
问题已经搞定
作者:
远祥
时间:
2019-6-8 20:20
这样都是C语言吗
作者:
未来第一站
时间:
2019-6-8 22:28
进阶功能^_^
欢迎光临 机械必威体育网址 (//www.szfco.com/)
Powered by Discuz! X3.4