• This forum is the machine-generated translation of www.cad3d.it/forum1 - the Italian design community. Several terms are not translated correctly.

macro for separate replacement

  • Thread starter Thread starter Sp4rk0
  • Start date Start date

Sp4rk0

Guest
Bye to all,
I wanted to know if there is a macro or a way to automatically replace a separate, I have various models of distinct materials and at the moment I delete the existing table and insert a new one, I wanted to know if there is a possibility to tell solid to replace the table materials a with a b, so then I can throw the macro on multiple files and fix them in block.

Thank you.
 
I don't think you can replace it. I've never seen this possibility in the bees. I think the macro should remake the difference as you do now.
 
Hi.
I think this macro is your case.

to use it you must change the value of bom_path with the complete path of the table model you want to insert (.sldbomtbt).

when you launch the macro on a drawing, look for a material table in each sheet of the file. if in the sheet is present, then it is deleted, then the new one is inserted in the same position. It's a bit of an eye, but not a programmer, I can't do better.

I hope it helps.
Code:
' insertbom
'
' insert or replace bom table in existing drawing with the specified template

' bom table template path
const bom_path as string = "c:\...\tabella disegno.sldbomtbt"

dim swapp as sldworks.sldworks

dim postablex as double
dim postabley as double

sub main()

    set swapp = application.sldworks
 
    'table option
    dim anchortype as long
    anchortype = swbomconfigurationanchor_topleft
    dim bomtype as long
    bomtype = swbomtype_toplevelonly
    
    ' check if file is open, if it's saved and if it's a drawing
    dim swmodel as sldworks.modeldoc2
    set swmodel = swapp.activedoc

    if swmodel is nothing then
        msgbox ("aprire un documento per eseguire la macro")
        goto finally_
    end if

    if swmodel.getpathname() = "" then
        msgbox ("salvare il documento per eseguire la macro")
        goto finally_
    end if

    if not swmodel.gettype() = swdocdrawing then
        msgbox ("aprire un file di disegno per eseguire la macro")
        goto finally_
    end if
    
    dim swdrawing  as sldworks.drawingdoc
    set swdrawing = swmodel
    
    dim swmodeldocext as sldworks.modeldocextension
    set swmodeldocext = swmodel.extension
    
    ' find sheet first view
    dim firstview as sldworks.view
    set firstview = swdrawing.getfirstview
    
    do while not firstview is nothing
        if not firstview.type = swdrawingviewtypes_e.swdrawingsheet then
            exit do
        end if
        set firstview = firstview.getnextview
    loop
    
    ' code from https://www.codestack.net/solidworks-api/document/tables/find-tables-by-type/
    dim vtables as variant
    vtables = findtables(swdrawing, array(swtableannotationtype_e.swtableannotation_billofmaterials))
    
    dim strconfiguration as string
    strconfiguration = firstview.referencedconfiguration
    
    dim tabletemplate as string
    tabletemplate = bom_path
    
    dim swbomannotation
    
    dim findtabletest as boolean
    findtabletest = false
    
    if not isempty(vtables) then
        
        dim i as integer
        
        for i = 0 to ubound(vtables)

            findtabletest = true
            
            dim swtablebom as sldworks.bomtableannotation
            set swtablebom = vtables(i)
            
            dim swbomfeature as sldworks.bomfeature
            set swbomfeature = swtablebom.bomfeature
            
            dim swtable as sldworks.tableannotation
            set swtable = vtables(i)
            
            dim swannotation as annotation
            set swannotation = swtable.getannotation
            
            dim posbom as variant
            posbom = swannotation.getposition
            
            ' 'print table position in console log
            ' for j = 0 to ubound(posbom)
                ' debug.print "coordinata " & j & ": " & round(posbom(j)*1000,0)
            ' next
            
            'save bom position
            postablex = posbom(0)
            postabley = posbom(1)
            
            ' select current bom and delete it
            dim swfeat as sldworks.feature
            set swfeat = swbomfeature.getfeature
            
            dim selret as boolean
            bret = swfeat.select2(false, 0)
            
            dim delstatus as boolean
            delstatus = swmodeldocext.deleteselection2(swdelete_children)
            
            ' place new bom
            set swbomannotation = firstview.insertbomtable4(false, postablex, postabley, anchortype, bomtype, strconfiguration, tabletemplate, false, swnumberingtype_detailed, false)
            
            set swbomfeature = swbomannotation.bomfeature
            swbomfeature.partconfigurationgrouping = swdisplay_allconfigurationofsamepart_asoneitem
            
            ' bom order
            dim swsortdata as bomtablesortdata
            set swsortdata = swbomannotation.getbomtablesortdata
            
            swsortdata.columnindex(0) = 0  ' primary sorting 1st column
            swsortdata.ascending(0) = true ' ascending
            
            swsortdata.sortmethod = swbomtablesortmethod_literal
            
            ' change number after order
            swsortdata.donotchangeitemnumber = false
            
            ' sort and save
            sortsaved = swsortdata.savecurrentsortparameters
            
            dim status as boolean
            status = swbomannotation.sort(swsortdata)

            status = swbomannotation.applysavedsortscheme(swsortdata)
            
        next

        if findtabletest = false then

            postablex = 0
            postabley = 0
            
            ' placebom firstview, postablex, postabley, strconfiguration

            set swbomannotation = firstview.insertbomtable4(false, postablex, postabley, anchortype, bomtype, strconfiguration, tabletemplate, false, swnumberingtype_detailed, false)

            dim bomfeat as sldworks.bomfeature
            set bomfeat = swbomannotation.bomfeature
            bomfeat.partconfigurationgrouping = swdisplay_allconfigurationofsamepart_asoneitem

            ' bom order
            dim sortdata as bomtablesortdata
            set sortdata = swbomannotation.getbomtablesortdata
            
            sortdata.columnindex(0) = 0  ' primary sorting 1st column
            sortdata.ascending(0) = true ' ascending
            
            sortdata.sortmethod = swbomtablesortmethod_literal
            
            ' change number after order
            sortdata.donotchangeitemnumber = false
            
            ' sort and save
            sortsaved = sortdata.savecurrentsortparameters
            
            dim check as boolean
            check = swbomannotation.sort(sortdata)

            check = swbomannotation.applysavedsortscheme(swsortdata)


        end if

        swdrawing.forcerebuild
        
    end if
    
finally_:
        
end sub

function findtables(draw as sldworks.drawingdoc, filter as variant) as variant
    
    dim swtables() as sldworks.tableannotation
    dim isinit as boolean
    isinit = false
    
    dim swsheet as sldworks.view
    set swsheet = draw.getfirstview
    
    dim vtableanns as variant
    vtableanns = swsheet.gettableannotations
    
    if not isempty(vtableanns) then
        
        dim j as integer
        
        for j = 0 to ubound(vtableanns)
            
            dim swtableann as sldworks.tableannotation
            set swtableann = vtableanns(j)
            
            if filtercontains(swtableann.type, filter) then
                
                if isinit then
                    redim preserve swtables(ubound(swtables) + 1)
                else
                    redim swtables(0)
                    isinit = true
                end if
                
                set swtables(ubound(swtables)) = swtableann
                
            end if
            
        next
        
    end if
    
    findtables = swtables
    
end function

function filtercontains(val as swtableannotationtype_e, filter as variant) as boolean
        
    dim i as integer
    
    for i = 0 to ubound(filter)
        if val = filter(i) then
            filtercontains = true
            exit function
        end if
    next
    
    filtercontains = false
    
end function

sub placebom(swview as sldworks.view, xpos as double, ypos as double, confiname as string)

    dim bomannotation as bomtableannotation
    set bomannotation = swview.insertbomtable4(false, xpos, ypos, anchortype, bomtype, confiname, bom_path, false, swnumberingtype_detailed, false)

    dim bomfeat as bomfeature
    set bomfeat = bomannotation.bomfeature
    bomfeat.partconfigurationgrouping = swdisplay_allconfigurationofsamepart_asoneitem

    ' bom order
    dim sortdata as bomtablesortdata
    set sortdata = bomannotation.getbomtablesortdata
    
    sortdata.columnindex(0) = 0  ' primary sorting 1st column
    sortdata.ascending(0) = true ' ascending
    
    sortdata.sortmethod = swbomtablesortmethod_literal
    
    ' change number after order
    sortdata.donotchangeitemnumber = false
    
    ' sort and save
    sortsaved = sortdata.savecurrentsortparameters
    
    dim check as boolean
    check = bomannotation.sort(sortdata)

    check = swbomannotation.applysavedsortscheme(swsortdata)


end sub
 

Forum statistics

Threads
44,997
Messages
339,767
Members
4
Latest member
ibt

Members online

No members online now.
Back
Top