Create a group from Excel list
Create a group from Excel list
(OP)
Hi,
I have a CATProduct open (500 parts), I have an Excel file that give me the name of 35 specific parts of this CATProduct. I would like a macro that could create a group with these 35 parts.
I have digged into the search options, I am thinking that a solution could be with personalized query, but I cannot find anything relevant.
I am using Catia V5, R20.
Thanks for your help,
Marc
I have a CATProduct open (500 parts), I have an Excel file that give me the name of 35 specific parts of this CATProduct. I would like a macro that could create a group with these 35 parts.
I have digged into the search options, I am thinking that a solution could be with personalized query, but I cannot find anything relevant.
I am using Catia V5, R20.
Thanks for your help,
Marc





RE: Create a group from Excel list
indocti discant et ament meminisse periti
RE: Create a group from Excel list
CODE -->
Sub Selecttt() Dim sPath As String CATIA.DisplayFileAlerts = False Path = "C:\Products.xlsx" Set xlApp = CreateObject("Excel.Application") Set MyXL = GetObject(, "Excel.Application") Set MyXL = GetObject(Path) MyXL.Application.Visible = True MyXL.Parent.Windows(1).Visible = True For i = 1 To 3 sPath = MyXL.Application.Sheets(1).Cells(i, 1).Value Set ActiveProductDocument = CATIA.ActiveDocument Set Product1 = ActiveProductDocument.Product Set Product3Products = Product1.Products Set Product2Dot1 = Product3Products.Item(sPath) Set ActiveProductDocument = CATIA.ActiveDocument Set selection1 = ActiveProductDocument.Selection selection1.Add Product2Dot1 Next i Dim myGroups Set myGroups = Product1.GetTechnologicalObject("Groups") Dim myGroup As Group Set myGroup = myGroups.AddFromSel CATIA.DisplayFileAlerts = True End SubRE: Create a group from Excel list
It looks like it's a bit more complicated. Indeed I use Catia on a UNIX station, so I would probably not be able to use this Excel sheet... Do you think I can instead use a txt file ?
I tried anyway to launch the macro, it stopped on the Line 29, column 6: "Next i", with the following error: "expected end of statement"
RE: Create a group from Excel list
CODE -->
Sub CATMain() Dim strFilePath As String Dim objFile As File Dim objTextStream As TextStream Dim strLine As String 'Display file open dialog strFilePath = CATIA.FileSelectionBox("Select Text File", "*.txt", 0) 'If user clicked cancel (empty string is returned), then exit the program If strFilePath = "" Then Exit Sub 'Open the file, add the 10 first parts to the selection1 Set objFile = CATIA.FileSystem.GetFile(strFilePath) Set objTextStream = objFile.OpenAsTextStream("ForReading") For i = 1 to 10 strLine = objTextStream.ReadLine Set ActiveProductDocument = CATIA.ActiveDocument Set Product1 = ActiveProductDocument.Product Set Product3Products = Product1.Products Set Product2Dot1 = Product3Products.Item(strLine) '<<< Here is where it stop "The method Item failed" Set ActiveProductDocument = CATIA.ActiveDocument Set selection1 = ActiveProductDocument.Selection selection1.Add Product2Dot1 Next objTextStream.Close End SubDo you have any idea how to solve this ?
RE: Create a group from Excel list
RE: Create a group from Excel list
I also tried with the PN, I tried with the name as displayed in graph, same thing...
RE: Create a group from Excel list
one way to know.. change the number of iterations to 9.. and check if the error appears.
or maybe with message boxes..
RE: Create a group from Excel list
CODE -->
Sub CATMain() Set ActiveProductDocument = CATIA.ActiveDocument Set Product1 = ActiveProductDocument.Product Set Product3Products = Product1.Products Set Product2Dot1 = Product3Products.Item("XXX") Set ActiveProductDocument = CATIA.ActiveDocument Set selection1 = ActiveProductDocument.Selection selection1.Add Product2Dot1 End SubAny name I am using give me the same error "The method Item failed". I cannot figure out how to use this ".Item".
RE: Create a group from Excel list
indocti discant et ament meminisse periti
RE: Create a group from Excel list
You may want to check also this, there is an example code there which is doing what you want...when you are using "name" is in fact Instance Name.
And agree with AlexLozoya, in CATScript use MsgBox to check your code.
Regards
Fernando
https://picasaweb.google.com/102257836106335725208
https://picasaweb.google.com/103462806772634246699...
RE: Create a group from Excel list
One last issue: I don't know how to add a selection to another selection. In the code you provided, the selection selPN is changing at each loop of the "For Each strLine in arrLines". But how can I store this information and add it to another selection ?
I thought of using a collection (like cnc07 in this Post) but the declaration "As New Collection" give me a error "Expected end of statement".
Thanks,
Marc
RE: Create a group from Excel list
I finally managed to do what I wanted, it's working on my windows PC. But it's giving me the an error on my UNIX station, without any description of the error:
CODE --> CATScript
Sub CATMain() Dim productDocument1 As Document Set productDocument1 = CATIA.ActiveDocument Dim selPN, selGL As Selection Set selPN = productDocument1.Selection Set selGL = productDocument1.Selection Dim Producttemp As Product Dim objFSO, strTextFile, strData, strLine, arrLines CONST ForReading = 1 'name of the text file strTextFile = "H:\test.txt" 'Create a File System Object Set objFSO = CreateObject("Scripting.FileSystemObject") '<<< HERE IS WHERE THE MACRO STOPS AND GIVE ME AN ERROR 'Open the text file - strData now contains the whole file strData = objFSO.OpenTextFile(strTextFile,ForReading).ReadAll 'Split the text file into lines arrLines = Split(strData,vbCrLf) '~ 'Step through the lines For Each strLine in arrLines '~ '''''''''''''''''''''''''''''''''''''''' selPN.Search "Name=" & "*" & strLine & "*" & " ,all" SelPN.VisProperties.SetRealColor 0, 0, 255, 1 Next SelGL.Search "Color=Blue,all" 'Cleanup objFSO Set objFSO = Nothing End SubDoes anybody has an idea ?
Thanks
RE: Create a group from Excel list
Check your file path (line strTextFile = "H:\test.txt") , in UNIX is different then in WIndows. This could be one reason why is not working.
Regards
Fernando
https://picasaweb.google.com/102257836106335725208
https://picasaweb.google.com/103462806772634246699...
RE: Create a group from Excel list
Actually the code is stopping at the next line:
CODE --> CATScript
'Create a File System Object Set objFSO = CreateObject("Scripting.FileSystemObject")RE: Create a group from Excel list
Instead of
'Create a File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject") '<<< HERE IS WHERE THE MACRO STOPS AND GIVE ME AN ERROR
....................
....................
Use
set fs = CATIA.FileSystem
dim strTextFile as File
set strTextFile = fs.GetFile("/Myserver/myfolder/test.txt")
MsgBox strTextFile
dim ts as TextStream
set ts = strTextFile.OpenAsTextStream("ForReading")
do while ts.atendofstream=false
''''''.code to read the lines
loop
Regards
Fernando
https://picasaweb.google.com/102257836106335725208
https://picasaweb.google.com/103462806772634246699...
RE: Create a group from Excel list
I am going to work on this, and come back here to get you a feedback.
RE: Create a group from Excel list
CODE --> CATScript
Sub CATMain() On error resume next Dim productDocument1 As Document Set productDocument1 = CATIA.ActiveDocument Dim selPN, selGL As Selection Set selPN = productDocument1.Selection Set selGL = productDocument1.Selection Dim Producttemp As Product Set fs = CATIA.FileSystem Dim strTextFile as File set strTextFile = fs.GetFile(CATIA.FileSelectionBox("Select Text File","*.txt",0)) Dim ts as TextStream set ts = strTextFile.OpenAsTextStream("ForReading") Do Dim Lire as String Lire = ts.ReadLine MsgBox Lire selPN.Search "Name=" & "*" & Lire & "*" & " ,all" SelPN.VisProperties.SetRealColor 0, 0, 255, 1 Loop while ts.atendofstream=false SelGL.Search "Color=Blue,all" SelGL.VisProperties.SetRealColor 0, 0, 255, 0 'Cleanup objFSO Set objFSO = Nothing End SubThanks for your help !