why does it not finish create the Txt file ?

why does it not finish create the Txt file ?

Darkforce_the_ilogic_guy
Advisor Advisor
769 Views
5 Replies
Message 1 of 6

why does it not finish create the Txt file ?

Darkforce_the_ilogic_guy
Advisor
Advisor

I have this code this should write all the part number into an part files .. but for some reason it stop in the middle or it. antone know why and how to fix it ?

 

Sub Main()
	oWrite = System.IO.File.CreateText(ThisDoc.PathAndFileName(False) & ".txt")
Dim oAsmDoc As AssemblyDocument
oAsmDoc = ThisApplication.ActiveDocument

Dim oRefDocs As DocumentsEnumerator
oRefDocs = oAsmDoc.AllReferencedDocuments
Dim oRefDoc As Document

Dim wrongDisplayname As String


For Each oRefDoc In oRefDocs
	Try
		
	
			debug("1")
			
			oWrite.WriteLine(iProperties.Value(oRefDoc.DisplayName, "Project", "Part Number") )
			wrongDisplayname = wrongDisplayname &  vbLf & vbLf & " " & oRefDoc.FullDocumentName.ToString
			
			 
		
			
		
	Catch
	
End Try
Next

MessageBox.Show(wrongDisplayname, "Title")


End Sub


Public Sub debug(txt As String)
	Trace.WriteLine("NTI : Colour " & txt)
End Sub

 

 

it stop in the midle off one

 

 

0 Likes
770 Views
5 Replies
Replies (5)
Message 2 of 6

Michael.Navara
Advisor
Advisor

Using & for concatenation of strings is not good idea

wrongDisplayname = wrongDisplayname &  vbLf & vbLf & " " & oRefDoc.FullDocumentName.ToString
			

Use StringBuilder instead

 

Dim oStringBuilder as New StringBuilder
...
oStringBuilder.AppendLine(oRefDoc.FullDocumentName)
...
MsgBox(oStringBuilder.ToString())

 

0 Likes
Message 3 of 6

Darkforce_the_ilogic_guy
Advisor
Advisor
Sub Main()
	Dim oString As String = "<?xml version=""1.0""?>"
	Dim oStringBuilder As New StringBuilder
	
	
	
	oWrite = System.IO.File.CreateText(ThisDoc.PathAndFileName(False) & ".txt")
Dim oAsmDoc As AssemblyDocument
oAsmDoc = ThisApplication.ActiveDocument

Dim oRefDocs As DocumentsEnumerator
oRefDocs = oAsmDoc.AllReferencedDocuments
Dim oRefDoc As Document

Dim wrongDisplayname As String



oStringBuilder.AppendLine(oString)

oWrite.WriteLine(oString)
 'oWrite.WriteLine("<?xml version=\"1.0\"?>")
'oWrite.WriteLine("<SearchParameters xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" TypeName="Connectivity.Explorer.Document.FileExplorerObject" LatestOnly="True" SearchSubfolders="True" SearchFullContent="False" IsFolder="True" Id="bc434c5c-206f-4b21-ba14-2a9d5587e1a8" xmlns="http://schemas.autodesk.com/msd/plm/SearchParameters/2009-07-23">")
'oWrite.WriteLine("  <SearchConditions>")
For Each oRefDoc In oRefDocs
	Try
		
	'If oRefDoc.DisplayNameOverridden = True 
			debug("1")
			'oWrite.WriteLine(oRefDoc.FullDocumentName.ToString)
			'oWrite.WriteLine(iProperties.Value(oRefDoc.DisplayName, "Project", "Part Number") + " OR ")
			oStringBuilder.AppendLine(iProperties.Value(oRefDoc.DisplayName, "Project", "Part Number") + " OR ")
			'wrongDisplayname = wrongDisplayname &  vbLf & vbLf & " " & oRefDoc.FullDocumentName.ToString
			
			 
		'End If
			
		
	Catch
	
End Try






Next
oWrite.WriteLine(oStringBuilder.ToString())

	
MessageBox.Show(wrongDisplayname, "Title")


End Sub


Public Sub debug(txt As String)
	Trace.WriteLine("NTI : Colour " & txt)
End Su

 

 

I try to make it that way , now I get an error the stringBuilder is not defind 

0 Likes
Message 4 of 6

Michael.Navara
Advisor
Advisor

try this full rule

 

Sub Main
	Dim oAsmDoc As AssemblyDocument
	oAsmDoc = ThisApplication.ActiveDocument

	Dim oRefDocs As DocumentsEnumerator = oAsmDoc.AllReferencedDocuments
	Dim oRefDoc As Document

	Dim partNumbers As New System.Text.StringBuilder
	Dim fileNames As New System.Text.StringBuilder

	For Each oRefDoc In oRefDocs
		Try
			debug("1")
			Dim partNumber As String = oRefDoc.PropertySets("{32853F0F-3444-11D1-9E93-0060B03C1CA6}")("Part Number").Value.ToString()
			partNumbers.AppendLine(partNumber)
			fileNames.AppendLine(oRefDoc.FullDocumentName)
		Catch
			Logger.Error("failed " & oRefDoc.DisplayName)
		End Try
	Next
	System.IO.File.WriteAllText(ThisDoc.PathAndFileName(False) & ".txt", partNumbers.ToString())
	MessageBox.Show(fileNames.ToString(), "Title")
End Sub


Public Sub debug(txt As String)
	Trace.WriteLine("NTI : Colour " & txt)
End Sub
0 Likes
Message 5 of 6

Darkforce_the_ilogic_guy
Advisor
Advisor

it does not do all I need it to do

 

I need first to inset

<?xml version="1.0"?>
<SearchParameters xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" TypeName="Connectivity.Explorer.Document.FileExplorerObject" LatestOnly="true" SearchSubfolders="true" SearchFullContent="false" IsFolder="true" Id="bc434c5c-206f-4b21-ba14-2a9d5587e1a8" xmlns="http://schemas.autodesk.com/msd/plm/SearchParameters/2009-07-23">
<SearchConditions>
<SearchCondition Value="

then

 

all the part number like this

 

Partnumer1 OR

Partnumber2 OR

partnumber 3 

 

after all the part number I need to add this

 

 

" PromptForValue="false" PropertyKey="PartNumber">
<Type>CONTAINS</Type>
</SearchCondition>
<SearchCondition PromptForValue="false" PropertyKey="HasDrawing">
<Type>FALSE</Type>
</SearchCondition>
</SearchConditions>
<SearchLocations>
<Locations URI="vaultfolderpath:$" />
</SearchLocations>
</SearchParameters>

 

 

 

 

 

 

0 Likes
Message 6 of 6

Michael.Navara
Advisor
Advisor

You can use function for formatting string. Similar to this example: 

Sub main
	GetSearchStringTest()
End Sub
Sub GetSearchStringTest()
	Dim partNumbers As New List(Of String)
	partNumbers.Add("partNumber1")
	partNumbers.Add("partNumber2")
	partNumbers.Add("partNumber3")
	Dim result = GetSearchString(partNumbers)

	MsgBox(result)
End Sub

Private Function GetSearchString(partNumbers As IEnumerable(Of String)) As String
	Dim searchStringFormat As String =
            "<?xml version=""1.0""?><SearchParameters xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" TypeName=""Connectivity.Explorer.Document.FileExplorerObject"" LatestOnly=""True"" SearchSubfolders=""True"" SearchFullContent=""False"" IsFolder=""True"" Id=""bc434c5c-206f-4b21-ba14-2a9d5587e1a8"" xmlns=""http://schemas.autodesk.com/msd/plm/SearchParameters/2009-07-23""> <SearchConditions> <SearchCondition Value=""{0}"" PromptForValue=""False"" PropertyKey=""PartNumber""> <Type>CONTAINS</Type> </SearchCondition> <SearchCondition PromptForValue=""False"" PropertyKey=""HasDrawing""> <Type>False</Type> </SearchCondition> </SearchConditions> <SearchLocations> <Locations URI=""vaultfolderpath:$"" /> </SearchLocations> </SearchParameters>"
	Dim partNumbersJoined = String.Join(" ", partNumbers)
	Return String.Format(searchStringFormat, partNumbersJoined)
End Function

 

 

0 Likes