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
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
Making the best use of this Forum. FAQ559-716
How to get answers to your SW questions. FAQ559-1091
Helpful SW websites every user should be aware of. FAQ559-520
RE: Getting BOM from SW
swModel = swApp.ActiveDoc returns nothing
RE: Getting BOM from SW
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
RE: Getting BOM from SW
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
Regg
RE: Getting BOM from SW
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
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
Thanks,
Alex
RE: Getting BOM from SW
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
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
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