机械社区

 找回密码
 注册会员

QQ登录

只需一步,快速开始

搜索
查看: 3271|回复: 5

solidworks自己录制的VBA代码有问题

[复制链接]
发表于 2019-6-8 14:12:22 | 显示全部楼层 |阅读模式
本帖最后由 jinjunbai 于 2019-6-8 14:17 编辑
- C. m8 T; z* s1 H4 {. X* Y( s( Y/ m5 H' @" K6 m. [6 h
今天尝试用VBA代码完成一个图形的绘制,发现程序自己录制的VBA执行都有问题,比如基准面,绘图的时候设置好,VBA中执行出来就没有了,请高手帮忙解决一下
8 i. u% ^' ^, [4 {3 w* ~7 I9 r' s( \2 V
代码如下:
/ l0 H* x) m4 v1 q) h' ******************************************************************************/ B! J2 Z! C. b# |6 C+ h
' C:\Users\admin\AppData\Local\Temp\swx11724\Macro1.swb - macro recorded on 06/08/19 by admin
& v( I$ k0 \" Z, D$ I# T3 _; ?" X0 h' ******************************************************************************) z5 v! \! E0 k0 {) r
Dim swApp As Object* D" \) @7 ^: J

; d- P6 S( T9 Q* ^Dim Part As Object
( h& a5 l6 [: }1 N+ ]Dim boolstatus As Boolean# b7 a$ z/ \- [' g# I" {3 L
Dim longstatus As Long, longwarnings As Long: E4 M: U, V) Y

7 }  _2 I3 S9 }  j4 x4 p' YSub main()
8 u' ~; {  t+ {5 z+ l. `
; Y+ R* D" M& o! o. Q( ^Set swApp = Application.SldWorks* b9 m' Q6 L( U9 ^+ x- X
% [" F6 \# |8 v

# }; ]/ N( W0 N6 s5 P' New Document4 a. s+ [% q" I( P7 }- }
Dim swSheetWidth As Double
' v* q/ k: c, ^* {; y* sswSheetWidth = 0. W; z3 W# Z4 n+ }  E* u# ~. J0 k7 q* h
Dim swSheetHeight As Double# g$ V. v3 Y( x" b/ g, c. l
swSheetHeight = 0
" p) z* N& ]( E* Z7 Q6 ~0 PSet Part = swApp.NewDocument("C:\ProgramData\SolidWorks\SOLIDWORKS 2018\templates\gb_part.prtdot", 0, swSheetWidth, swSheetHeight)
7 r! N* A% I7 r: NDim swPart As PartDoc
3 L/ d8 F8 \4 Y& q2 qSet swPart = Part, p1 H8 \: r- A* F% W3 T$ x
swApp.ActivateDoc2 "零件1", False, longstatus
8 x% x$ d6 e& ~Set Part = swApp.ActiveDoc
& ~$ l8 [" R. Z, d* J$ E5 h1 {! FDim myModelView As Object% I% A- d. M/ j0 b0 p; e$ w
Set myModelView = Part.ActiveView
( f+ U9 U4 x$ ~: H& |  Y* vmyModelView.FrameState = swWindowState_e.swWindowMaximized
! F# `/ Q( `6 V3 z! qboolstatus = Part.Extension.SelectByID2("注解", "DCABINET", 0, 0, 0, False, 0, Nothing, 0)
( e4 }) f& b( v, M8 G3 V: Oboolstatus = Part.Extension.SelectByID2("前视基准面", "PLANE", 0, 0, 0, False, 0, Nothing, 0)/ k# Y7 l. u: I
Part.SketchManager.InsertSketch True
( H4 x- d* E4 k9 t% K- W# ~, oPart.ClearSelection2 True
# [+ V* o- c1 G/ \8 d  ]4 _boolstatus = Part.Extension.SetUserPreferenceToggle(swUserPreferenceToggle_e.swSketchAddConstToRectEntity, swUserPreferenceOption_e.swDetailingNoOptionSpecified, False)' K' y- ]2 k8 u% M6 j
boolstatus = Part.Extension.SetUserPreferenceToggle(swUserPreferenceToggle_e.swSketchAddConstLineDiagonalType, swUserPreferenceOption_e.swDetailingNoOptionSpecified, True)8 \- q! |' r8 D% g  y, r
Dim vSkLines As Variant
/ ?4 f  v0 Q5 S, A/ j4 _9 C; T) v% }vSkLines = Part.SketchManager.CreateCornerRectangle(-4.03305583756345E-02, 3.97460575296108E-02, 0, 6.89710998307952E-02, -0.03010179357022, 0)1 N+ U4 o3 h& [& T5 A2 ~" D9 n
: q- B! x% Z: I
' Named View+ j1 t, }* T; E" o
Part.ShowNamedView2 "*上下二等角轴测", 8
. V  a1 e; L8 C; o8 CPart.ViewZoomtofit2
: ^7 Z3 w+ ]" b2 i3 Q0 O: C: a- cDim myFeature As Object9 K# L4 }2 ~4 g. f
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)
- ?2 i8 \' a& F  @4 G3 J* b. K: S& A9 TPart.SelectionManager.EnableContourSelection = False/ C7 N1 e  L5 D* h
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)# h) f, F8 b: k3 b
Part.ClearSelection2 True7 M5 S7 G8 N* u- F
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)
/ E0 f4 |. _% p5 @) r+ Z5 [Part.ClearSelection2 True
2 p( B& x( s7 {" i! p* Hboolstatus = Part.Extension.SelectByID2("前视基准面", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
+ j5 U$ [; N- y( @1 w) x$ ?# eboolstatus = Part.Extension.SelectByID2("前视基准面", "PLANE", 0, 0, 0, True, 0, Nothing, 0)
+ H6 x* g, ~* W+ k8 ]+ m5 SDim myRefPlane As Object
( N$ v5 [: \& {3 E+ `Set myRefPlane = Part.FeatureManager.InsertRefPlane(8, 0.01, 0, 0, 0, 0)
" I5 c5 {3 m8 [0 A0 ], L6 T* GPart.ClearSelection2 True; ?8 |0 {& L5 c2 ?
boolstatus = Part.Extension.SelectByID2("前视基准面", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
# ^# M( ]7 \3 A. \: BPart.ClearSelection2 True  B' R- Y. U! l/ l' \) c
Part.ClearSelection2 True8 q  t7 S0 C6 o  U
boolstatus = Part.Extension.SetUserPreferenceToggle(swUserPreferenceToggle_e.swSketchAddConstToRectEntity, swUserPreferenceOption_e.swDetailingNoOptionSpecified, False)
! @) q: H$ r& v. _5 h% i5 X6 rboolstatus = Part.Extension.SetUserPreferenceToggle(swUserPreferenceToggle_e.swSketchAddConstLineDiagonalType, swUserPreferenceOption_e.swDetailingNoOptionSpecified, True)
" m" _  g( b$ Q$ evSkLines = Part.SketchManager.CreateCornerRectangle(-1.26249913529932E-02, 1.98473013094258E-02, 0, 4.43244050501335E-02, -1.64793375533918E-02, 0)
. o( E7 ]0 v% a6 }  }' d# C7 ZSet 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)
: Q6 Z% f) m+ V5 j8 QPart.SelectionManager.EnableContourSelection = False5 j% i. ?% v2 t8 }2 O/ \, M
End Sub1 Z% S) @' {, G( ~4 @4 T4 {
% Y0 u* k' v* t3 t

- t* i2 y8 ^8 S  s; Z- S' X4 {

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册会员

x
回复

使用道具 举报

发表于 2019-6-8 16:00:55 | 显示全部楼层
SW录制的部分动作不会记录,需要对二次开发的语句有一定了解才能修改,建议看一下API帮助文档入门后再提问。
回复 支持 反对

使用道具 举报

发表于 2019-6-8 16:29:15 | 显示全部楼层
先说清楚自己想实现什么动作
6 s* Q! \3 s. A# Z% x- T, h
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-6-8 16:49:25 | 显示全部楼层
问题已经搞定
回复 支持 反对

使用道具 举报

发表于 2019-6-8 20:20:08 | 显示全部楼层
这样都是C语言吗
回复 支持 反对

使用道具 举报

发表于 2019-6-8 22:28:52 | 显示全部楼层
进阶功能^_^
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

小黑屋|手机版|Archiver|机械社区 ( 京ICP备10217105号-1,京ICP证050210号,浙公网安备33038202004372号 )

GMT+8, 2024-11-25 22:55 , Processed in 0.055017 second(s), 17 queries , Gzip On.

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表