×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Getting BOM from SW

Getting BOM from SW

Getting BOM from SW

(OP)
Hi I am extremely new to sw programming and I am having some troubles. Here is my code in VB.Net

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim swApp As SldWorks.SldWorks
        Dim swBomTable As SldWorks.BomTableAnnotation
        Dim fileerror As Long
        Dim filewarning As Long
        Dim swView As SldWorks.View
        Dim swModel As SldWorks.ModelDoc2
        Dim swSelMgr As SldWorks.SelectionMgr
        Dim swError As Long

        swApp = CreateObject("SldWorks.Application")


        Try
            swModel = swApp.ActivateDoc2("C:\IF 53914.slddrw", True, swError)
            swSelMgr = swModel.SelectionManager
            swView = swSelMgr.GetSelectedObject5(1)
            swBomTable = swView.GetBomTable

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub


I always get the OBJECT REFERENCE IS NOT SET TO AN INSTANCE OF AN OBJECT,  because swView is NOTHING. Selection manager report 0 selected objects in the debug view.

any help will be highly appreciated

Thanx
Iggy

RE: Getting BOM from SW

(OP)
I looked at that code as well, but I am still having essentially the same problem. I have a drawing open, yet
swModel = swApp.ActiveDoc returns nothing

RE: Getting BOM from SW

To use the selection manager, I believe you have to select the view first.  Although I must admit I have never tried this.  However, this is how normally retrieve the BOM table

Set swView = swModel.GetFirstView

Set swTable = swView.GetFirstTableAnnotation

You then have to check if swTable is = to Nothing.  IF not then check the type of table returned examble:

swTable.Type = swTableAnnotation_BillOfMaterials

If that fails then you have to check the next view on the drawing

Set swView = swView.GetNextView

and start all over from

Set swTable = swView.GetFirstTableAnnotation

Do this until you find the BOM you are looking for or you run out of views.

If I remember correctly, the first view has the sheet format on it so you will cycle through at least once.

Regards,

Regg

RE: Getting BOM from SW

(OP)
First of all thank you for your explanations. They have shed some light on the whole issue. Some problems still persist though:

here is the code:

Try
'swApp.OpenDoc6("X:\Iggy\Basic Assembly\basicdwg.slddrw", swDocDRAWING, _
'swOpenDocOptions_Silent, "", fileerror, filewarning)

swModel = swApp.ActiveDoc
swDraw = swModel
swView = swDraw.GetFirstView
Do While Not swView Is Nothing
swTable = swView.GetFirstTableAnnotation
If Not swTable Is Nothing Then
MsgBox(swTable.Title)
End If
'swBomTable = swView.GetBomTable
Do While Not swTable Is Nothing
ProcessTable(swApp, swModel, swTable)
swTable = swTable.GetNext
Loop
swView = swView.GetNextView
Loop

Catch ex As Exception
MsgBox(ex.Message)
End Try

problem is that the swTable or swBOMTAble for that matter are always nothing. I iterate through 4 views yet the table is never set. I am using an excel based BOM.

RE: Getting BOM from SW

I no longer work with Excel BOMs.  I am not sure what I gave you works with those.

Regg

RE: Getting BOM from SW

ronin2307,

Try using

swTable = swView.GetBOMTable

According to API help this will return the Excel BOM table.

If you go into API help do a search on BOMTable::, it will return a list of APIs that are used along with it.

Regards,

Regg

RE: Getting BOM from SW

(OP)
thanx. After much headache and research I have found out why I was not successful before.
ATTACH3 method is what I was failing to do, therefore I couldn't retrieve any data from the table.
I have it working now. Thanx

RE: Getting BOM from SW

Please post your successful VB code, I too am working with Excel BOM and seek to manipulate this externally.

Thanks,
Alex

RE: Getting BOM from SW

(OP)
here is most of the code. A bit messy but it does the job
If there are questions please ask and I'll be happy to explain.
We have a few different templates and therefore I have to check the column headers accordingly so I know what to do with the data and in which column I can expect what. Bunch of formatting etc, that is really not that valuable to you.

CODE

For Each path As String In arrFilePaths
                intCount += 1
                arrFilePathAndMultiplier = path.Split(",")
                lblFile.Text = "Processing File: " & intCount & " of " & arrFilePaths.Count
               
                swApp.OpenDoc6(arrFilePathAndMultiplier(0), swDocDRAWING, swOpenDocOptions_ReadOnly, "", fileerror, filewarning)
                intMultiplier = arrFilePathAndMultiplier(1)

                blBOM_FOUND = False

                'getFileName
                GetFileName(path, strFileName)

                swModel = swApp.ActiveDoc

                If swModel Is Nothing Then
                    arlErrors.Add(path)
                Else
                    swDraw = swModel

                    'gotta do something here to get to the first sheet
                    'assuming for now that the first entry in the names array is actually the first sheet in the drawing
                    arrSheetNames = swDraw.GetSheetNames()
                    swDraw.ActivateSheet(arrSheetNames(0))

                    'start the do while loop that will iterate through the sheets if multiple present
                    Do While blBOM_FOUND = False
                        'here I get the paper size info
                        sheet = swDraw.GetCurrentSheet
                        retval = sheet.GetProperties
                        'strSheetName = sheet.GetName
                        arrDbl = retval

                        'Write a function here that will send the drawing size as a parameter so that I can set up the printer

                        If arrDbl(0) = PaperSize.A Or arrDbl(0) = PaperSize.B Then
                            PrinterSetup(swModel, ps, arrDbl(0), False)
                            PrinterSetup(swModel, ps, arrDbl(0), False)
                            PrinterSetup(swModel, ps, arrDbl(0), False)
                        ElseIf arrDbl(0) = PaperSize.C Or arrDbl(0) = PaperSize.D Then
                            PrinterSetup(swModel, ps, PaperSize.B, True)
                            PrinterSetup(swModel, ps, PaperSize.B, True)
                            PrinterSetup(swModel, ps, arrDbl(0), False)
                        End If


                        swView = swDraw.GetFirstView
                        arrdata.Clear()
                        Do While Not swView Is Nothing
                            'can I get the title here???
                            strTitle = swModel.GetTitle
                            swTbl = swView.GetBomTable
                            name = swView.Name
                            BOM_TYPE = BOMType.UNKNOWN_BOM
                            If Not swTbl Is Nothing Then 'ASSUME FIRST BOM FOUND IS THE ONE TO USE
                                blBOM_FOUND = True
                                bRet = swTbl.Attach3 ' activate the table
                                count_column = swTbl.GetColumnCount
                                count_row = swTbl.GetRowCount()
                                
                'DO YOUR PROCESSING OF THE DATA HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!                


                                swTbl.Detach() 'release the table
                            End If
                            swView = swView.GetNextView
                        Loop

                        
                        If Not blBOM_FOUND Then
                            swDraw.SheetNext()
                        End If
                    Loop

                    'update bar
                    ProgressBar1.Value += 1
                    'update form
                    Repaint()

                    swApp.QuitDoc(strTitle)

                End If

Next

RE: Getting BOM from SW

It looks like you are only reading from the BOM and not writing to it. Are you able to write to the BOM and change BOM table data and have the changes show up after you detach from the BOM? My BOMs happen to contain macros that read model dimensions and do not always update properly after a rebuild. Instead, they display as blank cells. Manually double clicking on the table and then off again updates it.

Using Attach3 and then Detach looks like its doing the same thing since the cells appear to have updated text in them when the Excel object is active, but as soon as the BOM is detached, they blank out again. I can even change data programatically in non macro driven cells using the Excel object and it displays briefly, but then goes blank again after detaching.

Have you by chance dealt with this issue?

RE: Getting BOM from SW

(OP)
sorry, I haven't ever had to write to the BOM so I really do not have any idea how to do it.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources