几何尺寸与公差论坛

 找回密码
 注册
查看: 796|回复: 0

【转帖】help needed for solidwork macro11

[复制链接]
发表于 2009-4-13 11:43:41 | 显示全部楼层 |阅读模式
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!!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|小黑屋|几何尺寸与公差论坛

GMT+8, 2024-12-23 22:16 , Processed in 0.039999 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2023 Discuz! Team.

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