Your Products have been synced, click here to refresh
Thanks, it's working just fine now. However I do have another question. Currently this changes only the path in my print command. I have paths called out in other commands as well, script, stats, assignments, file/open, file/close, etc. I've been able to get those to change using this script by copy/paste and changing some code.
Is there a way to just straight up look for a string of text ("J:\") throughout the whole program, like find/replace, without looking for a specific command? I only ask because not all programs have the same commands. I would need to modify this script to include all commands where a path is called out. I don't have a problem doing this; I understand if a program does not have that command it will skip over it. I'm just curious...
Try pcdParts.Open(FilePath, "CMM1") pcdActivePart = pcdApp.ActivePartProgram pcdCommands = pcdActivePart.Commands For i As Integer = 0 To pcdCommands.Count pcdCommand = pcdCommands.Item(i) If pcdCommand Is Nothing Then Continue For [COLOR="#008000"]'If Not pcdCommand.Type = PCDLRN.OBTYPE.PRINT_REPORT Then Continue For[/COLOR] Dim tmpText = pcdCommand.GetText(PCDLRN.ENUM_FIELD_TYPES.FILE_NAME, 1) If tmpText.Contains(ChangeFrom) Then pcdCommand.PutText(tmpText.Replace(ChangeFrom, ChangeTo), PCDLRN.ENUM_FIELD_TYPES.FILE_NAME, 1) End If Next pcdActivePart.Close() Return True Catch ex As Exception Console.WriteLine("ConvertPrintPath : " & ex.Message) Return False End Try
Imports System.Threading Imports System.IO Module Module1 Const ProgramDir = "J:\TEMP\" Const ProgramFileExt = "*.PRG" Const ChangeFromPath = "J:\" Const ChangeToPath = "S:\" Private pcdApp As PCDLRN.Application Private pcdParts As PCDLRN.PartPrograms Private pcdActivePart As PCDLRN.PartProgram Private pcdCommands As PCDLRN.Commands Private pcdCommand As PCDLRN.Command Sub Main() If ConnectPCD() = True Then Dim tmpFiles() As String = GetFiles(ProgramDir) Try For Each tmpFile As String In tmpFiles If ConvertPrintPath(tmpFile, ChangeFromPath, ChangeToPath) = False Then Exit For Next Catch ex as Exception Console.Writeline(ex.Message) End Try Console.WriteLine("Complete!") End If DisconnectPCD() Console.ReadLine() End Sub Function GetFiles(ByVal RootDir As String) As String() Return Directory.GetFiles(RootDir, ProgramFileExt, SearchOption.AllDirectories) End Function Function ConnectPCD() As Boolean Try pcdApp = GetObject("", "PCDLRN.Application") pcdApp.WaitUntilReady(60) Thread.Sleep(2000) pcdApp.Visible = True pcdApp.Maximize() pcdParts = pcdApp.PartPrograms If pcdParts.Count <> 0 Then Throw New Exception("Please close all open PC-DMIS part programs.") End If Return True Catch ex As Exception Console.WriteLine("ConnectPCD : " & ex.Message) Return False pcdParts = Nothing pcdApp = Nothing GC.Collect() End Try End Function Sub DisconnectPCD() pcdCommand = Nothing pcdCommands = Nothing pcdActivePart = Nothing pcdParts = Nothing pcdApp = Nothing GC.Collect() End Sub Function ConvertPrintPath(ByVal FilePath As String, ByVal ChangeFrom As String, ByVal ChangeTo As String) As Boolean Console.WriteLine("Converting file: " & FilePath) Try pcdParts.Open(FilePath, "CMM1") pcdActivePart = pcdApp.ActivePartProgram pcdCommands = pcdActivePart.Commands For i As Integer = 0 To pcdCommands.Count pcdCommand = pcdCommands.Item(i) If pcdCommand Is Nothing Then Continue For [COLOR="#008000"]'If Not pcdCommand.Type = PCDLRN.OBTYPE.PRINT_REPORT Then Continue For[/COLOR] Dim tmpText = pcdCommand.GetText(PCDLRN.ENUM_FIELD_TYPES.FILE_NAME, 1) If tmpText.Contains(ChangeFrom) Then pcdCommand.PutText(tmpText.Replace(ChangeFrom, ChangeTo), PCDLRN.ENUM_FIELD_TYPES.FILE_NAME, 1) End If Next pcdActivePart.Close() Return True Catch ex As Exception Console.WriteLine("ConvertPrintPath : " & ex.Message) Return False End Try End Function End Module
Thanks, it's working just fine now. However I do have another question. Currently this changes only the path in my print command. I have paths called out in other commands as well, script, stats, assignments, file/open, file/close, etc. I've been able to get those to change using this script by copy/paste and changing some code.
Is there a way to just straight up look for a string of text ("J:\") throughout the whole program, like find/replace, without looking for a specific command? I only ask because not all programs have the same commands. I would need to modify this script to include all commands where a path is called out. I don't have a problem doing this; I understand if a program does not have that command it will skip over it. I'm just curious...
Try pcdParts.Open(FilePath, "CMM1") pcdActivePart = pcdApp.ActivePartProgram pcdCommands = pcdActivePart.Commands For i As Integer = 0 To pcdCommands.Count pcdCommand = pcdCommands.Item(i) If pcdCommand Is Nothing Then Continue For [COLOR="#008000"]'If Not pcdCommand.Type = PCDLRN.OBTYPE.PRINT_REPORT Then Continue For[/COLOR] Dim tmpText = pcdCommand.GetText(PCDLRN.ENUM_FIELD_TYPES.FILE_NAME, 1) If tmpText.Contains(ChangeFrom) Then pcdCommand.PutText(tmpText.Replace(ChangeFrom, ChangeTo), PCDLRN.ENUM_FIELD_TYPES.FILE_NAME, 1) End If Next pcdActivePart.Close() Return True Catch ex As Exception Console.WriteLine("ConvertPrintPath : " & ex.Message) Return False End Try
Imports System.Threading Imports System.IO Module Module1 Const ProgramDir = "J:\TEMP\" Const ProgramFileExt = "*.PRG" Const ChangeFromPath = "J:\" Const ChangeToPath = "S:\" Private pcdApp As PCDLRN.Application Private pcdParts As PCDLRN.PartPrograms Private pcdActivePart As PCDLRN.PartProgram Private pcdCommands As PCDLRN.Commands Private pcdCommand As PCDLRN.Command Sub Main() If ConnectPCD() = True Then Dim tmpFiles() As String = GetFiles(ProgramDir) Try For Each tmpFile As String In tmpFiles If ConvertPrintPath(tmpFile, ChangeFromPath, ChangeToPath) = False Then Exit For Next Catch ex as Exception Console.Writeline(ex.Message) End Try Console.WriteLine("Complete!") End If DisconnectPCD() Console.ReadLine() End Sub Function GetFiles(ByVal RootDir As String) As String() Return Directory.GetFiles(RootDir, ProgramFileExt, SearchOption.AllDirectories) End Function Function ConnectPCD() As Boolean Try pcdApp = GetObject("", "PCDLRN.Application") pcdApp.WaitUntilReady(60) Thread.Sleep(2000) pcdApp.Visible = True pcdApp.Maximize() pcdParts = pcdApp.PartPrograms If pcdParts.Count <> 0 Then Throw New Exception("Please close all open PC-DMIS part programs.") End If Return True Catch ex As Exception Console.WriteLine("ConnectPCD : " & ex.Message) Return False pcdParts = Nothing pcdApp = Nothing GC.Collect() End Try End Function Sub DisconnectPCD() pcdCommand = Nothing pcdCommands = Nothing pcdActivePart = Nothing pcdParts = Nothing pcdApp = Nothing GC.Collect() End Sub Function ConvertPrintPath(ByVal FilePath As String, ByVal ChangeFrom As String, ByVal ChangeTo As String) As Boolean Console.WriteLine("Converting file: " & FilePath) Try pcdParts.Open(FilePath, "CMM1") pcdActivePart = pcdApp.ActivePartProgram pcdCommands = pcdActivePart.Commands For i As Integer = 0 To pcdCommands.Count pcdCommand = pcdCommands.Item(i) If pcdCommand Is Nothing Then Continue For [COLOR="#008000"]'If Not pcdCommand.Type = PCDLRN.OBTYPE.PRINT_REPORT Then Continue For[/COLOR] Dim tmpText = pcdCommand.GetText(PCDLRN.ENUM_FIELD_TYPES.FILE_NAME, 1) If tmpText.Contains(ChangeFrom) Then pcdCommand.PutText(tmpText.Replace(ChangeFrom, ChangeTo), PCDLRN.ENUM_FIELD_TYPES.FILE_NAME, 1) End If Next pcdActivePart.Close() Return True Catch ex As Exception Console.WriteLine("ConvertPrintPath : " & ex.Message) Return False End Try End Function End Module
© 2024 Hexagon AB and/or its subsidiaries. | Privacy Policy | Cloud Services Agreement |