From: Dames on
I am inurgent need of a macro. This macro needs to add a new bom then
i have some other code to that will print this bom. Then i need the
macro to delete the bom that was paced in the drawing, but not delete
any other bom that was already in the drawing. Can anyone help me or
point me in the right direction. I must say that i don't have a great
deal of scripting ability.
Cheers
Damian.

From: Philippe Guglielmetti on
Start by recording a macro while you do everything manually. This will
build the skeletton of your macro. It probably won't play back, but
it's a good start since it will contain many of the required API
calls.

The big question then is : what changes between 2 runs of your macro,
or between 2 drawings on which it will run ? Do you need to select
items ? do you need to format bom columns differently ?

Programming the macro consists mostly in "generalizing" it, making it
able to handle different cases on different documents. And this is
generally the hard part.

Maybe you could post your recorded macro and answer the question
above : what changes between your "temporary boms" on different
drawings. Then we might give you some more help here.

From: Dames on
On Jul 22, 5:39 pm, Philippe Guglielmetti <goo...(a)goulu.net> wrote:
> Start by recording a macro while you do everything manually. This will
> build the skeletton of your macro. It probably won't play back, but
> it's a good start since it will contain many of the required API
> calls.
>
> The big question then is : what changes between 2 runs of your macro,
> or between 2 drawings on which it will run ? Do you need to select
> items ? do you need to format bom columns differently ?
>
> Programming the macro consists mostly in "generalizing" it, making it
> able to handle different cases on different documents. And this is
> generally the hard part.
>
> Maybe you could post your recorded macro and answer the question
> above : what changes between your "temporary boms" on different
> drawings. Then we might give you some more help here.

Q1. I am export an advanced BOM in an XML format. The drawings typicly
have a BOM that only has about 6 feilds, but my advanced BOM will have
about 15-20. I need this BOM only as an export not to be on the
drawing..

I tried to record, but there dosn't reaaly seem to be anything of use
in the code. See below.


'
******************************************************************************
' C:\DOCUME~1\DGILLE~1\LOCALS~1\Temp\swx1256\Macro1.swb - macro
recorded on 07/23/07 by dgillespie
'
******************************************************************************
Dim swApp As Object
Dim Part As Object
Dim SelMgr As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim Feature As Object
Sub main()

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc
Set SelMgr = Part.SelectionManager
boolstatus = Part.Extension.SelectByID2("DetailItem147(a)Sheet2",
"ANNOTATIONTABLES", -0.07991769230769, 0.08582843076923, 0, False, 0,
Nothing, 0)
Part.EditDelete
End Sub


Anyway here is my code.
'------------------------------------------------

'

' Preconditions:

' (1) Drawing document is open.

' (2) Drawing contains at least one BOM.

' (3) In VBA, add a reference to Microsoft Scripting Runtime
using

' Tools, References (C:\windows\system32\scrrun.dll)

'

' Postconditions: XML file is saved to the same directory,

' overwriting any existing file of the same name.

'

' NOTES:

' (1) XML tags are based on BOM column headings.

' (2) Invalid characters must be removed from the

' column headings.

' (3) Microsoft Scripting Runtime reference must be selected in
VBA.

' (4) XML schema is:

'

' <BOMS>

' <SHEET>

' <NAME>Sheet1</NAME>

' <BOM>

' <NAME>Bill Of Materials1</NAME>

' <TABLE>

' <ROW>

' <ITEM_NO>1</ITEM_NO>

' <PART_NUMBER>2004_Part</PART_NUMBER>

' <DESCRIPTION></DESCRIPTION>

' <QTY>2</QTY>

' </ROW>

' <ROW>

' <ITEM_NO>2</ITEM_NO>

' <PART_NUMBER>2004_Part</PART_NUMBER>

' <DESCRIPTION></DESCRIPTION>

' <QTY>1</QTY>

' </ROW>

' <ROW>

' <ITEM_NO>3</ITEM_NO>

' <PART_NUMBER>2004_Part</PART_NUMBER>

' <DESCRIPTION></DESCRIPTION>

' <QTY>1</QTY>

' </ROW>

' <ROW>

' <ITEM_NO>4</ITEM_NO>

' <PART_NUMBER>bead7</PART_NUMBER>

' <DESCRIPTION></DESCRIPTION>

' <QTY>1</QTY>

' </ROW>

' </TABLE>

' </BOM>

' </SHEET>

' <SHEET>

' <NAME>Sheet2</NAME>

' <BOM>

' <NAME>Bill Of Materials2</NAME>

' <TABLE>

' <ROW>

' <ITEM_NO>1</ITEM_NO>

' <PART_NUMBER>Assem3</PART_NUMBER>

' <DESCRIPTION></DESCRIPTION>

' <QTY>1</QTY>

' </ROW>

' <ROW>

' <ITEM_NO></ITEM_NO>

' <PART_NUMBER> cylinder</PART_NUMBER>

' <DESCRIPTION></DESCRIPTION>

' <QTY>1</QTY>

' </ROW>

' <ROW>

' <ITEM_NO></ITEM_NO>

' <PART_NUMBER> cylinder</PART_NUMBER>

' <DESCRIPTION></DESCRIPTION>

' <QTY>1</QTY>

' </ROW>

' <ROW>

' <ITEM_NO></ITEM_NO>

' <PART_NUMBER> SimpleCube_A</
PART_NUMBER>

' <DESCRIPTION></DESCRIPTION>

' <QTY>1</QTY>

' </ROW>

' <ROW>

' <ITEM_NO></ITEM_NO>

' <PART_NUMBER> JoinedCyl</
PART_NUMBER>

' <DESCRIPTION></DESCRIPTION>

' <QTY>1</QTY>

' </ROW>

' </TABLE>

' </BOM>

' <BOM>

' <NAME>Bill Of Materials3</NAME>

' <TABLE>

' <ROW>

' <ITEM_NO>8</ITEM_NO>

' <PART_NUMBER>2004_Part</PART_NUMBER>

' <DESCRIPTION></DESCRIPTION>

' <QTY>2</QTY>

' </ROW>

' </TABLE>

' <TABLE>

' <ROW>

' <ITEM_NO>9</ITEM_NO>

' <PART_NUMBER>2004_Part</PART_NUMBER>

' <DESCRIPTION></DESCRIPTION>

' <QTY>1</QTY>

' </ROW>

' <ROW>

' <ITEM_NO>10</ITEM_NO>

' <PART_NUMBER>2004_Part</PART_NUMBER>

' <DESCRIPTION></DESCRIPTION>

' <QTY>1</QTY>

' </ROW>

' </TABLE>

' <TABLE>

' <ROW>

' <ITEM_NO>11</ITEM_NO>

' <PART_NUMBER>bead7</PART_NUMBER>

' <DESCRIPTION></DESCRIPTION>

' <QTY>1</QTY>

' </ROW>

' </TABLE>

' </BOM>

' <BOM>

' <NAME>Bill Of Materials4</NAME>

' <TABLE>

' <TITLE>BOM Table 2</TITLE>

' <ROW>

' <ITEM_NO>1</ITEM_NO>

' <PART_NUMBER>cylinder</PART_NUMBER>

' <DESCRIPTION></DESCRIPTION>

' <QTY>1</QTY>

' </ROW>

' <ROW>

' <ITEM_NO>2</ITEM_NO>

' <PART_NUMBER>cylinder</PART_NUMBER>

' <DESCRIPTION></DESCRIPTION>

' <QTY>1</QTY>

' </ROW>

' </TABLE>

' <TABLE>

' <TITLE>BOM Table 2</TITLE>

' <ROW>

' <ITEM_NO>3</ITEM_NO>

' <PART_NUMBER>SimpleCube_A</
PART_NUMBER>

' <DESCRIPTION></DESCRIPTION>

' <QTY>1</QTY>

' </ROW>

' <ROW>

' <ITEM_NO>4</ITEM_NO>

' <PART_NUMBER>JoinedCyl</PART_NUMBER>

' <DESCRIPTION></DESCRIPTION>

' <QTY>1</QTY>

' </ROW>

' </TABLE>

' </BOM>

' </SHEET>

' </BOMS>

'

'

'----------------------------------------------

Option Explicit

Public Enum swTableSplitDirection_e

swTableSplit_None = 0

swTableSplit_Horizontal = 1

swTableSplit_Vertical = 2

End Enum

Sub ProcessTableAnn(swApp As SldWorks.SldWorks, swModel As
SldWorks.ModelDoc2, swTableAnn As SldWorks.TableAnnotation, XMLfile As
Scripting.TextStream)

Dim nNumRow As Long

Dim nNumCol As Long

Dim nNumHeader As Long

Dim sHeaderText() As String

Dim i As Long

Dim j As Long

Dim k As Long



Dim nIndex As Long

Dim nCount As Long

Dim nStart As Long

Dim nEnd As Long

Dim nSplitDir As Long



nNumHeader = swTableAnn.GetHeaderCount: Debug.Assert nNumHeader >=
1



nSplitDir = swTableAnn.GetSplitInformation(nIndex, nCount, nStart,
nEnd)

If swTableSplit_None = nSplitDir Then

Debug.Assert 0 = nIndex

Debug.Assert 0 = nCount

Debug.Assert 0 = nStart

Debug.Assert 0 = nEnd

nNumRow = swTableAnn.RowCount

nNumCol = swTableAnn.ColumnCount

nStart = nNumHeader

nEnd = nNumRow - 1

Else

Debug.Assert swTableSplit_Horizontal = nSplitDir

Debug.Assert nIndex >= 0

Debug.Assert nCount >= 0

Debug.Assert nStart >= 0

Debug.Assert nEnd >= nStart

nNumCol = swTableAnn.ColumnCount

If 1 = nIndex Then

' Add header offset for first portion of table

nStart = nStart + nNumHeader

End If

End If



XMLfile.WriteLine " <TABLE>"



If swTableAnn.TitleVisible Then





XMLfile.WriteLine " <TITLE>" & swTableAnn.Title
& "</TITLE>"

End If



ReDim sHeaderText(nNumCol - 1)



For j = 0 To nNumCol - 1

sHeaderText(j) = swTableAnn.GetColumnTitle(j)



' Replace invalid characters for XML tags

sHeaderText(j) = Replace(sHeaderText(j), ".", "")

sHeaderText(j) = Replace(sHeaderText(j), " ", "_")
sHeaderText(j) = Replace(sHeaderText(j), "(", "_")
sHeaderText(j) = Replace(sHeaderText(j), ")", "_")

Next j



For j = nStart To nEnd

XMLfile.WriteLine " <ROW>"

For k = 0 To nNumCol - 1
XMLfile.WriteLine " " + "<" +
sHeaderText(k) + ">" + swTableAnn.Text(j, k) + "</" + sHeaderText(k) +
">"

Next k

XMLfile.WriteLine " </ROW>"

Next j



XMLfile.WriteLine " </TABLE>"

End Sub

Sub ProcessBomFeature(swApp As SldWorks.SldWorks, swModel As
SldWorks.ModelDoc2, swBomFeat As SldWorks.BomFeature, XMLfile As
Scripting.TextStream)

Dim swFeat As SldWorks.Feature

Dim vTableArr As Variant

Dim vTable As Variant

Dim swTable As SldWorks.TableAnnotation

Set swFeat = swBomFeat.GetFeature



XMLfile.WriteLine " <BOM>"

XMLfile.WriteLine " <NAME>" & swFeat.Name & "</NAME>"



vTableArr = swBomFeat.GetTableAnnotations

For Each vTable In vTableArr

Set swTable = vTable



ProcessTableAnn swApp, swModel, swTable, XMLfile

Next vTable

XMLfile.WriteLine " </BOM>"

End Sub

Sub main()

Dim MyAppID As Long

Dim swApp As SldWorks.SldWorks

Dim swModel As SldWorks.ModelDoc2

Dim swDraw As SldWorks.DrawingDoc

Dim swSheet As SldWorks.Sheet

Dim swFeat As SldWorks.Feature

Dim swBomFeat As SldWorks.BomFeature

Dim sPathName As String

Dim nNumSheet As Long

Dim nRetval As Long

Dim i As Long

Dim bIsFirstSheet As Boolean

Dim bRet As Boolean

Dim fso As Scripting.FileSystemObject

Dim XMLfile As Scripting.TextStream

'Dim swApp As Object
Dim Part As Object

Set swApp = Application.SldWorks

Set swModel = swApp.ActiveDoc

Set swDraw = swModel



bIsFirstSheet = True



' Strip off SolidWorks file extension (sldxxx)

' and add XML extension (xml)

sPathName = swModel.GetPathName

sPathName = Left(sPathName, Len(sPathName) - 6)

sPathName = sPathName + "xml"



Set fso = CreateObject("Scripting.FileSystemObject")

Set XMLfile = fso.CreateTextFile(sPathName, True)
XMLfile.WriteLine "<?xml version=""1.0"" encoding=""ISO8859-1"" ?
>"
XMLfile.WriteLine "<?xml-stylesheet type=""text/xsl"" href=""I:
\PROPERTIES\XMLTRANSFORM\xmltransform.xsl""?>"


XMLfile.WriteLine "<BOMS>"



Set swFeat = swModel.FirstFeature

Do While Not swFeat Is Nothing

If "DrSheet" = swFeat.GetTypeName Then

XMLfile.WriteLine " <SHEET>"

XMLfile.WriteLine " <NAME>" + swFeat.Name + "</
NAME>"



bIsFirstSheet = False

End If



If "BomFeat" = swFeat.GetTypeName Then

Set swBomFeat = swFeat.GetSpecificFeature2



ProcessBomFeature swApp, swModel, swBomFeat, XMLfile

End If



Set swFeat = swFeat.GetNextFeature

If Not swFeat Is Nothing Then

If "DrSheet" = swFeat.GetTypeName And Not bIsFirstSheet
Then

XMLfile.WriteLine " </SHEET>"

End If

End If

Loop



XMLfile.WriteLine " </SHEET>"

XMLfile.WriteLine "</BOMS>"

XMLfile.Close
'MyAppID = Shell("I:\PROPERTIES\Catten Output\SW_Output_Catten.exe",
1)

'AppActivate MyAppID



End Sub

'----------------------------------------------------


Cheers
Damian


From: Philippe Guglielmetti on
Oh your project is much more advanced than I expected from your first
post.
Well you're on the right track, you know how to program, XML and such
things, so what do you still need ?
Oh ! How to delete the temporary table you just created ?

'since you have
Dim swBomFeat As SldWorks.BomFeature
'then
swBomFeat.GetFeature.Select2(false,0) ' to select it in the feature
tree
swDraw.EditCut ' the quick and dirty way OR
swDraw.Extension.DeleteSelection2(0)' the clean way, with options to
delete feature children.

I wrote a macro similar to yours some time ago... 2 comments:
1) If you export to XML, you don't really need to create a table, you
could build a bom "internally" by traversing the assembly, which gives
you even more flexibility than the SW BOM.
2) check the MSXML4 module to read/write XML files. You'll find plenty
of tutorials on the net. It's much more efficient than handling XML as
text files, as XML is more tricky than you expect. For example, your
macro might have trouble with my french éà accents or even worse,
quotes in part names and such things. Normally you have to parse all
strings you store in XML to convert them in a clean format with &quot;
for quotes ... MSXML takes all this pain out.

Regards

From: Dames on
On Jul 23, 4:43 pm, Philippe Guglielmetti <goo...(a)goulu.net> wrote:
> Oh your project is much more advanced than I expected from your first
> post.
> Well you're on the right track, you know how to program, XML and such
> things, so what do you still need ?
> Oh ! How to delete the temporary table you just created ?
>
> 'since you have
> Dim swBomFeat As SldWorks.BomFeature
> 'then
> swBomFeat.GetFeature.Select2(false,0) ' to select it in the feature
> tree
> swDraw.EditCut ' the quick and dirty way OR
> swDraw.Extension.DeleteSelection2(0)' the clean way, with options to
> delete feature children.
>
> I wrote a macro similar to yours some time ago... 2 comments:
> 1) If you export to XML, you don't really need to create a table, you
> could build a bom "internally" by traversing the assembly, which gives
> you even more flexibility than the SW BOM.
> 2) check the MSXML4 module to read/write XML files. You'll find plenty
> of tutorials on the net. It's much more efficient than handling XML as
> text files, as XML is more tricky than you expect. For example, your
> macro might have trouble with my french éà accents or even worse,
> quotes in part names and such things. Normally you have to parse all
> strings you store in XML to convert them in a clean format with &quot;
> for quotes ... MSXML takes all this pain out.
>
> Regards

Thanks for the vote of cofidence But i have nor really done this
program. I think i got most of this from some examples. So some help
would be great
Cheers
Damian