几何尺寸与公差论坛------致力于产品几何量公差标准GD&T (GDT:ASME)|New GPS(ISO)研究/CAD设计/CAM加工/CMM测量  


返回   几何尺寸与公差论坛------致力于产品几何量公差标准GD&T (GDT:ASME)|New GPS(ISO)研究/CAD设计/CAM加工/CMM测量 » 仿射空间:CAX软件开发(三)二次开发与程序设计 » CAD二次开发 » SolidWorks二次开发
用户名
密码
注册 帮助 会员 日历 银行 搜索 今日新帖 标记论坛为已读


回复
 
主题工具 搜索本主题 显示模式
旧 2009-04-13, 11:43 AM   #1
yang686526
高级会员
 
注册日期: 06-11
帖子: 14579
精华: 1
现金: 224494 标准币
资产: 234494 标准币
yang686526 向着好的方向发展
默认 【转帖】help needed for solidwork macro11

help needed for solidwork macro!!
hello all
i was looking for some help on macro in solidworks 2006.

actually, i have two layers on my solid works drawing. one is 'construction layer' and the other one is the 'default'. i was trying to write a macro that if i select a dimension and run the macro it's going to tell me that that certain dimension is on construction layer (or default).
i have tried to write a code as well, but s i am very new to this programming so i dont know what is wrong with it. i hope some one might be able to help me.
cheers
sub main()
dim swapp as sldworks.sldworks
dim swmodel as sldworks.modeldoc2
dim swdraw as sldworks.drawingdoc
dim swselmgr as sldworks.selectionmgr


dim swdisplayer as sldworks.layermgr


dim swdispdim as sldworks.displaydimension
dim swdim as sldworks.dimension
dim swdrawcomp as sldworks.drawingcomponent
dim bret as boolean
dim name as variant
dim swlayermgr as sldworks.layermgr
dim swlayer2 as sldworks.layer
dim swlayer as sldworks.layer

set swapp = application.sldworks
set swmodel = swapp.activedoc
set swlayermgr = swmodel.getlayermanager

set swdraw = swmodel
set swselmgr = swmodel.selectionmanager

set swlayer2 = swselmgr.getselectedobject6(1, 0)

' set swlayer = swlayer2.getcurrentlayer
' set swdim = swdispdim.getdimension
' set swlayer = swdisplayer.name


msgbox " current layer = " & swlayer2.name

end sub
wouldn't it be easier to just change the colour of the construction layer? then whatever is not that colour must be in the default layer.
what do you intend to do with the dimensions once you know which layer they are is on?

hi corblimeylimey
yea i am trying to make a macro when you select a dimension, it should check whether its on construction layer or default layer. suppose if its on construction layer the macro is going to change it to default layer. its a big macro to write so i though i would rahter do it in steps. so, the first step was to write a macro so if i select a dimension on a drawing and than run my macro, it should be able to tell me the name of the layer.
i hope someone can help me here, i have banged my head many times so far on the wall
thanks
what is your ultimate goal? to ensure all dimensions in a drawing are on the layer named "default"? does this help you at all?
code
const colorquery as boolean = false
const textlayercolor as long = 10944512
const dimslayercolor as long = 771752142
dim swapp as sldworks.sldworks
dim swdoc as sldworks.modeldoc2
dim swdwg as sldworks.drawingdoc
dim swview as sldworks.view
dim swannot as sldworks.annotation
dim smsg as string
dim swlyrmgr as sldworks.layermgr
dim swlayer as sldworks.layer
sub movedimsandtablestolayers()
set swapp = application.sldworks
set swdoc = swapp.activedoc
if swdoc.gettype <> swdocdrawing then
msgbox "this macro only works for drawing files."
exit sub
end if
set swdwg = swdoc
set swlyrmgr = swdoc.getlayermanager
if colorquery then
set swlayer = swlyrmgr.getlayer(swlyrmgr.getcurrentlayer)
msgbox "the color of layer " & swlayer.name & " is " & swlayer.color
exit sub
end if
set swlayer = swlyrmgr.getlayer("text")
swlayer.color = textlayercolor
set swlayer = swlyrmgr.getlayer("dims")
swlayer.color = dimslayercolor
set swview = swdwg.getfirstview
while not (swview is nothing)
set swannot = swview.getfirstannotation3
while not swannot is nothing
if swannot.gettype = swdisplaydimension then
swannot.layer = "dims"
elseif swannot.gettype = swnote then
swannot.layer = "text"
elseif swannot.gettype = swtableannotation then
swannot.layer = "text"
end if
set swannot = swannot.getnext3
wend
set swview = swview.getnextview
wend
set swlayer = nothing
set swlyrmgr = nothing
set swannot = nothing
set swview = nothing
set swdwg = nothing
set swdoc = nothing
set swapp = nothing
end sub
-handleman, cswp (the new, easy test)
hello handleman
sorry i think i havent explained what i am trying to do completely and clearly. actually, i got two layers in my drawing as i said before: construction and default. the construction layer got the dimensions which are just meant to be for the person who drew the drawing. the default layer is the one with the dimensions which are going to appear on the final printout. so the construction layer is going to be hidden most of the time.
suppose during drawing you have a particular dimension on it which you might think should be a construction dimension instead of a default one (or other way round i.e. it should be a default instead of a construction). so the user can click on that particular dimension and run the macro. the macro checks which layer that particular dimension is on and than give you the option of moving it to the other layer.
the code that you have send works well for getting the current layer. but i want a macro where i have to select a dimension first and than run a macro. i dont know how to use the selection manager so i can select a dimension and than use the properties of the layer to get the layer name of that particular dimension.
help!!
thanks
here's a tweak to some code i had already. you'll have to edit the number of spaces in one of the lines to get your layer names to line up with the buttons in a yes/no/cancel message box.
you don't have to pre-select a dimension. if one is not selected before you run the macro, the macro will clear selections until you pick a dimension.
code
option explicit
const lyr1 as string = "default"
const lyr2 as string = "construction"
const msg1 as string = "selected dimension is on layer """
const msg2 as string = """" & vbcrlf & "move to layer """
const minselections as long = 1
dim swapp as sldworks.sldworks
dim model as sldworks.modeldoc2
dim mydispdim as sldworks.displaydimension
dim mydim as sldworks.dimension
dim myannot as sldworks.annotation
dim selmgr as sldworks.selectionmgr
dim mydimvalue as variant
dim newval as double
dim resp as long
sub main()

set swapp = application.sldworks
set model = swapp.activedoc
set selmgr = model.selectionmanager

if not ((selmgr.getselectedobjectcount2(-1) = 1) and (selmgr.getselectedobjecttype3(1, -1) = swseldimensions)) then
swapp.sendmsgtouser2 "please select a dimension.", swmbwarning, swmbok
model.clearselection2 true
end if

while selmgr.getselectedobjecttype3(1, -1) <> swseldimensions

model.clearselection2 true 'clear all selections

while selmgr.getselectedobjectcount < minselections

doevents 'wait for user selection

wend

wend

set mydispdim = selmgr.getselectedobject6(1, -1)
set myannot = mydispdim.getannotation
select case myannot.layer
case lyr1
resp = msgbox(msg1 & myannot.layer & msg2 & lyr2 & """?", vbyesno)
if resp = vbyes then
myannot.layer = lyr2
if not myannot.layer = lyr2 then
msgbox "could not change layer!"
end if
end if
case lyr2
resp = msgbox(msg1 & myannot.layer & msg2 & lyr1 & """?", vbyesno)
if resp = vbyes then
myannot.layer = lyr1
if not myannot.layer = lyr1 then
msgbox "could not change layer!"
end if
end if
case else
resp = msgbox(msg1 & myannot.layer & vbcrlf & "choose yes, no or cancel below to " & _
vbcrlf & "change layer or keep current." & vbcrlf & vbcrlf & _
lyr1 & " " & lyr2 & " " & myannot.layer, vbyesnocancel)
'******************************************
'*******'^adjust number of spaces in this line to line up with buttons
'******************************************
select case resp
case vbyes
myannot.layer = lyr1
if not myannot.layer = lyr1 then
msgbox "could not change layer!"
end if
case vbno
myannot.layer = lyr2
if not myannot.layer = lyr2 then
msgbox "could not change layer!"
end if
end select
end select
end sub
-handleman, cswp (the new, easy test)
hey handleman
this is exactly what i was looking for. thank you very much for your help mate. really appreciate that.
have a nice weekend!!
yang686526离线中   回复时引用此帖
GDT自动化论坛(仅游客可见)
回复


主题工具 搜索本主题
搜索本主题:

高级搜索
显示模式

发帖规则
不可以发表新主题
不可以回复主题
不可以上传附件
不可以编辑您的帖子

vB 代码开启
[IMG]代码开启
HTML代码关闭



所有的时间均为北京时间。 现在的时间是 04:14 AM.


于2004年创办,几何尺寸与公差论坛"致力于产品几何量公差标准GD&T | GPS研究/CAD设计/CAM加工/CMM测量"。免责声明:论坛严禁发布色情反动言论及有关违反国家法律法规内容!情节严重者提供其IP,并配合相关部门进行严厉查处,若內容有涉及侵权,请立即联系我们QQ:44671734。注:此论坛须管理员验证方可发帖。
沪ICP备06057009号-2
更多