From: Bill Briggs on 27 Jul 2007 09:07 Well, I have been working on this for you this morning because I pretty much just "office space" it these days. I am almost done with it but I have to question your schema. First of all, I am not an expert on what a good schema is, I simply know how to create the xml file. So that said, are you sure this is the schema you want or are you open to some changes? If so, I will post back with some suggestions. Another thing I need to know is when I insert the temporary BOM, what type do you want? Top Level Only, Parts Only, or Indented? I will send you the complete program in VB.net and I will post the code here as well. It could be done with the macro editor but you would need to rewrite the xml creation stuff because mine uses vb.net libraries. Technically, you could write the xml file as a text document with a .xml extension. I do it with the libraries because it adds the formating for me automatically. Bill
From: Bill Briggs on 27 Jul 2007 12:03 This is a good start for you I think, I am using the "Custom Property View" as the view for the BOM. All you should have to change is the path to the BOM Template. This is set to work if a drawing is already open in SolidWorks. CreateBom is the main routine to start with. This code will run in VB.net with references to system.io, system.xml, SolidWorks Type Library, and SolidWorks Constant Library. I made a slight adjustment for now in your schema. After we discuss my previous post, I will make any adjustments you want and I will send you the compiled program so you can run it and I will send the entire solution so that if you get vb.net 2005 you can edit it and make it your own. The only thing I ask is that anyone who reads and this and likes what they see, remember me when you hear about job openings and/ or someone needing a custom application written. Bill ---------------------------- Module modBom Dim swApp As SldWorks.SldWorks Dim swModelDoc As SldWorks.ModelDoc2 Dim swDrawDoc As SldWorks.DrawingDoc Const strBomTemplate As String = "D:\Program Files\Common Files \Solidworks Data 2007\SWBOM.sldbomtbt" Dim MyXmlDoc As New XmlDocument Dim MyRoot As XmlNode Sub CreateBom() Try ' Get Active Model swModelDoc = GetActiveModelDoc() ' Verify Found If Not swModelDoc Is Nothing Then ' Verify is Drawing If swModelDoc.GetType = SwConst.swDocumentTypes_e.swDocDRAWING Then ' Get Drawing Doc swDrawDoc = swModelDoc ' Build Bom File Name Dim strFileName As String = Path.GetDirectoryName(swModelDoc.GetPathName) & "\" & Path.GetFileNameWithoutExtension(swModelDoc.GetPathName) & ".xml" ' Verify File does not exist and delete if file does exist If DeleteExistingFile(strFileName) = False Then MsgBox("Cannot overwrite existing file: " & strFileName, MsgBoxStyle.Critical, "Failed") Exit Sub End If ' Create Xml Writer Dim writer As New XmlTextWriter(strFileName, Nothing) 'Use indenting for readability. writer.Formatting = Formatting.Indented ' Write Start of Document" writer.WriteStartDocument(True) ' Create Root node MyRoot = MyXmlDoc.CreateElement("Boms") ' Read Drawing Bom ReadBom() ' Write Root node MyRoot.WriteTo(writer) ' Write End Document writer.WriteEndDocument() ' Flush Writer writer.Flush() ' Close Writer writer.Close() End If End If Catch ex As Exception End Try End Sub #Region " SolidWorks Document Tools " Function GetSolidWorks() As Boolean Try Dim MyReturn As Boolean = False If swApp Is Nothing Then swApp = GetObject("", "SldWorks.Application") End If If Not swApp Is Nothing Then MyReturn = True End If Return MyReturn Catch ex As Exception Return False End Try End Function Function GetActiveModelDoc() As SldWorks.ModelDoc2 Try ' Get SolidWorks Object GetSolidWorks() ' Get Active ModelDoc Object Dim SwModel As SldWorks.ModelDoc2 = swApp.ActiveDoc Return SwModel Catch ex As Exception Return Nothing End Try End Function #End Region #Region " Bom " Private Sub ReadBom() Try ' Get List Of Sheet Names Dim strSheets() As String = swDrawDoc.GetSheetNames() ' Process All Sheets For i As Integer = 0 To swDrawDoc.GetSheetCount - 1 Dim MySheetNode As XmlNode MySheetNode = MyXmlDoc.CreateElement("Sheet") Dim MyNode As XmlNode MyNode = MyXmlDoc.CreateElement("Name") MyNode.InnerText = strSheets(i) MySheetNode.AppendChild(MyNode) Dim MyBomNode As XmlNode = MyXmlDoc.CreateElement("BOM") ' Activate Sheet By Name swDrawDoc.ActivateSheet(strSheets(i)) ' Get BOM Drawing View Dim MyDrawView As SldWorks.View = swDrawDoc.GetFirstView ' Get Current Sheet Dim swSheet As SldWorks.Sheet = swDrawDoc.GetCurrentSheet ' Get Custom Property View Dim strView As String = swSheet.CustomPropertyView ' If the custom property view is the Default, Get the First View If strView = "Default" Then MyDrawView = swDrawDoc.GetFirstView MyDrawView = MyDrawView.GetNextView Else ' Find Matching View by Name MyDrawView = swDrawDoc.GetFirstView ' Loop Through Views While Not MyDrawView Is Nothing If MyDrawView.Name = strView Then Exit While End If ' Next View MyDrawView = MyDrawView.GetNextView End While End If ' Create Custom Xml Bom CreateXmlBom(MyDrawView, MyBomNode) ' Append To Sheet MySheetNode.AppendChild(MyBomNode) ' Append to Root MyRoot.AppendChild(MySheetNode) Next Catch ex As Exception End Try End Sub Sub CreateXmlBom(ByVal MyDrawView As SldWorks.View, ByRef MyBomNode As XmlNode) Try ' Insert and Get Bom Table Annotation Dim swBomTable As SldWorks.TableAnnotation = InsertBom(MyDrawView) ' Verify If Not swBomTable Is Nothing Then ' Process Bom ProcessBom(swBomTable, MyBomNode) ' Remove Bom Dim swAnnotation As SldWorks.Annotation = swBomTable.GetAnnotation swAnnotation.Select3(False, Nothing) swModelDoc.DeleteSelection(False) End If Catch ex As Exception End Try End Sub Sub ProcessBom(ByVal swBomTable As SldWorks.TableAnnotation, ByRef MyBomNode As XmlNode) Try ' Ensure Header is at Top swBomTable.SetHeader(SwConst.swTableHeaderPosition_e.swTableHeader_Top, 1) ' Process each Row For r As Integer = 1 To swBomTable.RowCount - 1 ' Create Row Node Dim MyRowNode As XmlNode = MyXmlDoc.CreateElement("ROW") ' Process each Column For c As Integer = 0 To swBomTable.ColumnCount - 1 ' Get Cell Value Dim strValue As String = swBomTable.DisplayedText(r, c) ' Get Header (Replace Spaces with "_", also the title cannot contain characters that are not allowed in xml node names) Dim strHeader As String = swBomTable.GetColumnTitle(c).Replace(" ", "_") ' Create Item Node Dim MyItemNode As XmlNode = MyXmlDoc.CreateElement(strHeader) ' Add Value MyItemNode.InnerText = strValue ' Append To Row MyRowNode.AppendChild(MyItemNode) Next ' Append to Bom MyBomNode.AppendChild(MyRowNode) Next Catch ex As Exception End Try End Sub Function InsertBom(ByVal MyDrawView As SldWorks.View) As SldWorks.TableAnnotation Try Dim MyReturn As SldWorks.TableAnnotation = Nothing ' Insert Bom Dim swBom As SldWorks.BomTableAnnotation = MyDrawView.InsertBomTable2(True, 0, 0, SwConst.swBOMConfigurationAnchorType_e.swBOMConfigurationAnchor_BottomLeft, SwConst.swBomType_e.swBomType_TopLevelOnly, MyDrawView.ReferencedConfiguration, strBomTemplate) ' Verify Bom If Not swBom Is Nothing Then ' Get Bom Feature Dim swBomFeature As SldWorks.BomFeature = swBom.BomFeature ' Verify If Not swBomFeature Is Nothing Then Dim MyVisibleConfigs As Object = Nothing Dim strConfigNames() As String = swBomFeature.GetConfigurations(False, MyVisibleConfigs) For i As Integer = 0 To strConfigNames.Length - 1 If strConfigNames(i) = MyDrawView.ReferencedConfiguration Then MyVisibleConfigs(i) = True swBomFeature.SetConfigurations(True, MyVisibleConfigs, strConfigNames) Exit For End If Next ' Get Bom Table For Each swBomTable As SldWorks.TableAnnotation In swBomFeature.GetTableAnnotations MyReturn = swBomTable Exit For Next End If End If Return MyReturn Catch ex As Exception Return Nothing End Try End Function Function DeleteExistingFile(ByVal strFileName As String) As Boolean Try Dim MyReturn As Boolean = False ' Check if File Exists If File.Exists(strFileName) = True Then ' Delete File File.Delete(strFileName) End If MyReturn = Not File.Exists(strFileName) Return MyReturn Catch ex As Exception Return False End Try End Function #End Region End Module
From: Wayne Tiffany on 27 Jul 2007 16:37 Where are you located? WT "Bill Briggs" <bbriggs(a)gpv.com> wrote in message news:1185552236.482685.76850(a)b79g2000hse.googlegroups.com... > This is a good start for you I think, I am using the "Custom Property > View" as the view for the BOM. All you should have to change is the > path to the BOM Template. This is set to work if a drawing is already > open in SolidWorks. CreateBom is the main routine to start with. > This code will run in VB.net with references to system.io, system.xml, > SolidWorks Type Library, and SolidWorks Constant Library. I made a > slight adjustment for now in your schema. After we discuss my > previous post, I will make any adjustments you want and I will send > you the compiled program so you can run it and I will send the entire > solution so that if you get vb.net 2005 you can edit it and make it > your own. The only thing I ask is that anyone who reads and this and > likes what they see, remember me when you hear about job openings and/ > or someone needing a custom application written. > > Bill > > ---------------------------- > Module modBom > > Dim swApp As SldWorks.SldWorks > Dim swModelDoc As SldWorks.ModelDoc2 > Dim swDrawDoc As SldWorks.DrawingDoc > Const strBomTemplate As String = "D:\Program Files\Common Files > \Solidworks Data 2007\SWBOM.sldbomtbt" > > Dim MyXmlDoc As New XmlDocument > Dim MyRoot As XmlNode > > Sub CreateBom() > > Try > > ' Get Active Model > swModelDoc = GetActiveModelDoc() > ' Verify Found > If Not swModelDoc Is Nothing Then > ' Verify is Drawing > If swModelDoc.GetType = SwConst.swDocumentTypes_e.swDocDRAWING > Then > ' Get Drawing Doc > swDrawDoc = swModelDoc > > ' Build Bom File Name > Dim strFileName As String = > Path.GetDirectoryName(swModelDoc.GetPathName) & "\" & > Path.GetFileNameWithoutExtension(swModelDoc.GetPathName) & ".xml" > ' Verify File does not exist and delete if file does exist > If DeleteExistingFile(strFileName) = False Then > MsgBox("Cannot overwrite existing file: " & strFileName, > MsgBoxStyle.Critical, "Failed") > Exit Sub > End If > > ' Create Xml Writer > Dim writer As New XmlTextWriter(strFileName, Nothing) > 'Use indenting for readability. > writer.Formatting = Formatting.Indented > ' Write Start of Document" > writer.WriteStartDocument(True) > ' Create Root node > MyRoot = MyXmlDoc.CreateElement("Boms") > > ' Read Drawing Bom > ReadBom() > > ' Write Root node > MyRoot.WriteTo(writer) > ' Write End Document > writer.WriteEndDocument() > ' Flush Writer > writer.Flush() > ' Close Writer > writer.Close() > > End If > End If > > Catch ex As Exception > > End Try > > End Sub > > #Region " SolidWorks Document Tools " > > Function GetSolidWorks() As Boolean > > Try > > Dim MyReturn As Boolean = False > > If swApp Is Nothing Then > swApp = GetObject("", "SldWorks.Application") > End If > > If Not swApp Is Nothing Then > MyReturn = True > End If > > Return MyReturn > Catch ex As Exception > Return False > End Try > > End Function > > Function GetActiveModelDoc() As SldWorks.ModelDoc2 > > Try > > ' Get SolidWorks Object > GetSolidWorks() > > ' Get Active ModelDoc Object > Dim SwModel As SldWorks.ModelDoc2 = swApp.ActiveDoc > > Return SwModel > Catch ex As Exception > Return Nothing > End Try > > End Function > > #End Region > > #Region " Bom " > > Private Sub ReadBom() > > Try > > ' Get List Of Sheet Names > Dim strSheets() As String = swDrawDoc.GetSheetNames() > ' Process All Sheets > For i As Integer = 0 To swDrawDoc.GetSheetCount - 1 > > Dim MySheetNode As XmlNode > MySheetNode = MyXmlDoc.CreateElement("Sheet") > > Dim MyNode As XmlNode > MyNode = MyXmlDoc.CreateElement("Name") > MyNode.InnerText = strSheets(i) > MySheetNode.AppendChild(MyNode) > Dim MyBomNode As XmlNode = MyXmlDoc.CreateElement("BOM") > > ' Activate Sheet By Name > swDrawDoc.ActivateSheet(strSheets(i)) > ' Get BOM Drawing View > Dim MyDrawView As SldWorks.View = swDrawDoc.GetFirstView > ' Get Current Sheet > Dim swSheet As SldWorks.Sheet = swDrawDoc.GetCurrentSheet > ' Get Custom Property View > Dim strView As String = swSheet.CustomPropertyView > ' If the custom property view is the Default, Get the First View > If strView = "Default" Then > MyDrawView = swDrawDoc.GetFirstView > MyDrawView = MyDrawView.GetNextView > Else > ' Find Matching View by Name > MyDrawView = swDrawDoc.GetFirstView > ' Loop Through Views > While Not MyDrawView Is Nothing > If MyDrawView.Name = strView Then > Exit While > End If > ' Next View > MyDrawView = MyDrawView.GetNextView > End While > End If > > ' Create Custom Xml Bom > CreateXmlBom(MyDrawView, MyBomNode) > > ' Append To Sheet > MySheetNode.AppendChild(MyBomNode) > ' Append to Root > MyRoot.AppendChild(MySheetNode) > > Next > > Catch ex As Exception > > End Try > > End Sub > > Sub CreateXmlBom(ByVal MyDrawView As SldWorks.View, ByRef MyBomNode > As XmlNode) > > Try > > ' Insert and Get Bom Table Annotation > Dim swBomTable As SldWorks.TableAnnotation = InsertBom(MyDrawView) > ' Verify > If Not swBomTable Is Nothing Then > ' Process Bom > ProcessBom(swBomTable, MyBomNode) > ' Remove Bom > Dim swAnnotation As SldWorks.Annotation = swBomTable.GetAnnotation > swAnnotation.Select3(False, Nothing) > swModelDoc.DeleteSelection(False) > End If > > Catch ex As Exception > > End Try > > End Sub > > Sub ProcessBom(ByVal swBomTable As SldWorks.TableAnnotation, ByRef > MyBomNode As XmlNode) > > Try > > ' Ensure Header is at Top > > swBomTable.SetHeader(SwConst.swTableHeaderPosition_e.swTableHeader_Top, > 1) > ' Process each Row > For r As Integer = 1 To swBomTable.RowCount - 1 > ' Create Row Node > Dim MyRowNode As XmlNode = MyXmlDoc.CreateElement("ROW") > ' Process each Column > For c As Integer = 0 To swBomTable.ColumnCount - 1 > ' Get Cell Value > Dim strValue As String = swBomTable.DisplayedText(r, c) > ' Get Header (Replace Spaces with "_", also the title cannot > contain characters that are not allowed in xml node names) > Dim strHeader As String = swBomTable.GetColumnTitle(c).Replace(" > ", "_") > ' Create Item Node > Dim MyItemNode As XmlNode = MyXmlDoc.CreateElement(strHeader) > ' Add Value > MyItemNode.InnerText = strValue > ' Append To Row > MyRowNode.AppendChild(MyItemNode) > Next > ' Append to Bom > MyBomNode.AppendChild(MyRowNode) > Next > Catch ex As Exception > > End Try > > End Sub > > Function InsertBom(ByVal MyDrawView As SldWorks.View) As > SldWorks.TableAnnotation > > Try > > Dim MyReturn As SldWorks.TableAnnotation = Nothing > > ' Insert Bom > Dim swBom As SldWorks.BomTableAnnotation = > MyDrawView.InsertBomTable2(True, 0, 0, > SwConst.swBOMConfigurationAnchorType_e.swBOMConfigurationAnchor_BottomLeft, > SwConst.swBomType_e.swBomType_TopLevelOnly, > MyDrawView.ReferencedConfiguration, strBomTemplate) > > ' Verify Bom > If Not swBom Is Nothing Then > ' Get Bom Feature > Dim swBomFeature As SldWorks.BomFeature = swBom.BomFeature > ' Verify > If Not swBomFeature Is Nothing Then > Dim MyVisibleConfigs As Object = Nothing > Dim strConfigNames() As String = > swBomFeature.GetConfigurations(False, MyVisibleConfigs) > For i As Integer = 0 To strConfigNames.Length - 1 > If strConfigNames(i) = MyDrawView.ReferencedConfiguration Then > MyVisibleConfigs(i) = True > swBomFeature.SetConfigurations(True, MyVisibleConfigs, > strConfigNames) > Exit For > End If > Next > ' Get Bom Table > For Each swBomTable As SldWorks.TableAnnotation In > swBomFeature.GetTableAnnotations > MyReturn = swBomTable > Exit For > Next > End If > End If > > Return MyReturn > Catch ex As Exception > Return Nothing > End Try > > End Function > > Function DeleteExistingFile(ByVal strFileName As String) As Boolean > > Try > Dim MyReturn As Boolean = False > > ' Check if File Exists > If File.Exists(strFileName) = True Then > ' Delete File > File.Delete(strFileName) > End If > > MyReturn = Not File.Exists(strFileName) > > Return MyReturn > Catch ex As Exception > Return False > End Try > > End Function > > #End Region > > End Module >
From: Bill Briggs on 27 Jul 2007 21:29 I am in Vassar, Michigan and you can see more info if you click on "view profile" by my name. Thanks for asking.
From: Dames on 28 Jul 2007 00:59 On Jul 28, 11:29 am, Bill Briggs <bbri...(a)gpv.com> wrote: > I am in Vassar, Michigan and you can see more info if you click on > "view profile" by my name. Thanks for asking. Thanks Bill. You have done a lot more than i thought anyone would do. Yes bill i am open to any ideas to make it better. Also idented assembly bom would be the best. It is Saturday arvo here so i will get back to this Monday morning my time. Cheers Bill. Damian
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 Prev: sldFuncFeat cannot be loaded ??? Next: XMLPropWorks enhancements question |