Autor
|
Thema: File SaveAs with new name and delete old files (5865 mal gelesen)
|
Sylas Mitglied
Beiträge: 322 Registriert: 19.11.2012 Dell Precision T3500 Intel Xeon W3550 @ 3,07 GHz 12 GB RAM CATIA V5 R28
|
erstellt am: 21. Nov. 2015 11:56 <-- editieren / zitieren --> Unities abgeben:
Hi Programmers! I'm trying to write a VBA macro, which would: -scan CATIA tree structure, -change some PartNames or some strings in PartNames with String defined by User in UserForm GUI -save all tree components with new PartNames + Main product (with change PartNumber too) under path defined by User in UserForm GUI VBA project attachted (please start it with Start module). It seems that I have problem with saving products, because I get the error-code by this line: Code: Sub SaveAll() doc_sznur = GUI.path.Value For Each Document In CATIA.Documents Select Case TypeName(Document) Case "PartDocument" Document.SaveAs (doc_sznur & "\" & Document.Product.PartNumber & ".CATPart") Case "ProductDocument" Document.SaveAs (doc_sznur & "\" & Document.Product.PartNumber & ".CATProduct") End SelectNext End Sub
and I don't know why... could someone please walk trough my project and tell me would could be improved? I've read also, that I should take care of InstanceNames trough CATSript instead of CATvba. Can someone explain me why and how? If something for further explaination is needed (CAD-structure perhaps) please let me know. Best regards Lucas Eine Antwort auf diesen Beitrag verfassen (mit Zitat/Zitat des Beitrags) IP |
joehz Moderator Freiberuflicher Konstrukteur
Beiträge: 1057 Registriert: 25.11.2006 Win7 Pro 64 + Ubuntu + Irix6.5.20 Dell Precision M6600 i7-2960XM 2.7GHz 16GB NVidia Quadro M5010 Catia V5R19 VB6Pro.SP6/VBA 6.5.1053
|
erstellt am: 22. Nov. 2015 17:46 <-- editieren / zitieren --> Unities abgeben: Nur für Sylas
Hi Lucas, since my Polish is not half as good as your English, I first had to translate the dialog. Please correct me, if I guessed badly :-) Before discussing your 'SaveAs' problem, I would like to address a few other obvious issues: Dim statements: Writing
Code:
Dim strType, doc_new_name, doc_old_name, doc_sznur As String
is not quite correct. The definition for Dims according to MS is: Zitat:
Dim [WithEvents] varname[([subscripts])] [As [New] type] [, [WithEvents] varname[([subscripts])] [As [New] type]] . . .
or shorter Zitat:
Dim varname [As type] , varname [As type] . . .
so your code should read Code:
Dim strType As String, doc_new_name As String, doc_old_name As String, doc_sznur As String
otherwise you are declaring strType, doc_new_name and doc_old_name as variants, that will be automatically typecast later. Modules:
You may, of course, create one module per sub-routine, but you do not have to. You could just as well put all the routines into one single module. Usually, if there are only a few routines, I put them all in one module. If I have very many in my program, I split them logically. Option Explicit:
Using 'Option Explicit' only in one module, but not in all, doesn't make sense. It's all or none. If you want to always use 'Option Explicit', set the option 'Require variable declaration' in the options menu. Code flow:
I would not recommend starting with a routine other than 'CatMain'. Instead of asking to first call 'Start', which, at the end, calls 'CatMain', why don't you write(pseudo-code): Sub CatMain call Start 'do the rest end sub No-one expects a macro to start with anything else than 'CatMain'. TextBox Text <-> value:
Usually I use the textbox property 'text' for assigning/reading the textbox contents. It seems more logical to me. Online help on the 'text' property states the following:
Zitat:
Returns or sets the text in a TextBox.
and further down: Zitat:
For a TextBox, any value you assign to the Text property is also assigned to the Value property.
Now to your 'SaveAs' issue:
- I rearranged your vba a bit, mostly according to the rules I set up above. - I did not touch module 'Instance_Rename'. - I only tested the routines behind 'Exit' and 'OK'. Check it out! Cheers, Joe ------------------ Inoffizielle Catia Hilfeseite Eine Antwort auf diesen Beitrag verfassen (mit Zitat/Zitat des Beitrags) IP |
Sylas Mitglied
Beiträge: 322 Registriert: 19.11.2012 Dell Precision T3500 Intel Xeon W3550 @ 3,07 GHz 12 GB RAM CATIA V5 R28
|
erstellt am: 22. Nov. 2015 20:52 <-- editieren / zitieren --> Unities abgeben:
Hi Joe Thank you for your analysis! Your comments are really useful, and I'll try to keep to them by this and my next projects. Your translation was really good! I'm impressed, and parallely sorry for my Polish words in my project As to your code: There's something wrong with the creation of the folder according to GUI.path.text - it seems, that this variable: Code: doc_sznur = GUI.path.Text
takes value of the initial folder, from which Product was opened, with the every iteration of the loop. In result, it doesn't create new folder, but saves new files under initial folder. Additionally, when I change name in only one Part or Product, only this particular part is saved within initial folder with new name, and all other parts are gone! In the end, this macro would be used to change name in the whole assembly, so that's not a problem. BUT if I would like to change only one part - it will delete all non-modified parts in initial folder - and that would be a disaster.....Could you help me with this too? Next thing - after SaveAs operation I've noticed, that main product has status: Modified under save management. I think is should be saved at the end. Maybe I shuld check somehow, if all children of the main product are saved. if they are- save main product. If not save all documents again with .Save? If my CATIA structure would have parts with linked geometry (I mean something was pasted with link) - what would happpen to those links after renaming parts and SaveAs operation? Would they be broken? Thanks in advance for your BIG help Lucas [Diese Nachricht wurde von Sylas am 22. Nov. 2015 editiert.] Eine Antwort auf diesen Beitrag verfassen (mit Zitat/Zitat des Beitrags) IP |
joehz Moderator Freiberuflicher Konstrukteur
Beiträge: 1057 Registriert: 25.11.2006 Win7 Pro 64 + Ubuntu + Irix6.5.20 Dell Precision M6600 i7-2960XM 2.7GHz 16GB NVidia Quadro M5010 Catia V5R19 VB6Pro.SP6/VBA 6.5.1053
|
erstellt am: 22. Nov. 2015 21:17 <-- editieren / zitieren --> Unities abgeben: Nur für Sylas
Hi Lucas, there's nothing wrong. I misunderstood your intention. :-) I assumed, that the idea was to - first read the old directory(folder) - then edit the folder - the save to the new folder If you do not change the folder name in the edit box, then it is still the original one. I never tried out the edit boxes for the sub-strings. Therefore I cannot say, if they work or not. Let's take a look at the program logic: You want to either - just change one or more part/products name & part number(the sub string), but keep the path - just save to a new path, without name changing - change names & number and save in a new folder Is this correct? So far, I only checked the 2.nd option. Zitat:
Next thing - after SaveAs operation I've noticed, that main product has status: Modified under save management. I think is should be saved at the end. Maybe I shuld check somehow, if all children of the main product are saved. if they are- save main product. If not save all documents again with .Save?
Or simply reverse the saving sequence: Last item first, first item (usually the product) last. Zitat:
If my CATIA structure would have parts with linked geometry (I mean something was pasted with link) - what would happpen to those links after renaming parts and SaveAs operation? Would they be broken?
Not sure. What happens, if you do that manually? Finally: When pressing 'Cancel' the dialog closes, passing control back to 'Catmain'. What is supposed to happen there? The renaming? Cheers, Joe ------------------ Inoffizielle Catia Hilfeseite Eine Antwort auf diesen Beitrag verfassen (mit Zitat/Zitat des Beitrags) IP |
Sylas Mitglied
Beiträge: 322 Registriert: 19.11.2012 Dell Precision T3500 Intel Xeon W3550 @ 3,07 GHz 12 GB RAM CATIA V5 R28
|
erstellt am: 23. Nov. 2015 10:34 <-- editieren / zitieren --> Unities abgeben:
Hi Let's clear some things. The logic is correct, but the second option is still not working for me. I've changed the textbox value in the GUI, but still the code doesn't create folder for me. Regarding the GUI buttons: Cancel - which supposed to be translated as Next -> should reset Sub-strings text boxes and show the GUI again (in case when user wants to have 2 different new sub-strings or wants to change some other sub-string in different Part). It should only rename parts, without any saving operation yet. OK - should close GUI and run the save & delete section Exit - cancels macro Check - supposed to be translated as browse should open folder browser (alternative option instead of manual typing of the new path)
Eine Antwort auf diesen Beitrag verfassen (mit Zitat/Zitat des Beitrags) IP |
joehz Moderator Freiberuflicher Konstrukteur
Beiträge: 1057 Registriert: 25.11.2006 Win7 Pro 64 + Ubuntu + Irix6.5.20 Dell Precision M6600 i7-2960XM 2.7GHz 16GB NVidia Quadro M5010 Catia V5R19 VB6Pro.SP6/VBA 6.5.1053
|
erstellt am: 23. Nov. 2015 10:54 <-- editieren / zitieren --> Unities abgeben: Nur für Sylas
Hi Lucas, Zitat:
The logic is correct, but the second option is still not working for me. I've changed the textbox value in the GUI, but still the code doesn't create folder for me.
It's not error tolerant. If the dir is not the immediate descendant of an existing one, it probably won't work. Furthermore: If you enter a folder name with a trailing backslash, it probably won't work. Zitat:
Regarding the GUI buttons:Cancel - which supposed to be translated as Next -> should reset Sub-strings text boxes and show the GUI again (in case when user wants to have 2 different new sub-strings or wants to change some other sub-string in different Part). It should only rename parts, without any saving operation yet.
I thought that 'Cancel' was wrong. It wouldn't make sense. Zitat:
OK - should close GUI and run the save & delete section Exit - cancels macro
Which they do. Zitat:
Check - supposed to be translated as browse should open folder browser (alternative option instead of manual typing of the new path)
As of now: Does nothing. I would prefer this option. No typos, no impossible dirs. And change the textbox to a label. About the saving options: Do you only want to replace changed files? Or be able to write everything to a new directory? Or...? Still guessing here. Cheers, Joe ------------------ Inoffizielle Catia Hilfeseite Eine Antwort auf diesen Beitrag verfassen (mit Zitat/Zitat des Beitrags) IP |
Sylas Mitglied
Beiträge: 322 Registriert: 19.11.2012 Dell Precision T3500 Intel Xeon W3550 @ 3,07 GHz 12 GB RAM CATIA V5 R28
|
erstellt am: 23. Nov. 2015 12:12 <-- editieren / zitieren --> Unities abgeben:
Zitat: About the saving options: Do you only want to replace changed files? Or be able to write everything to a new directory? Or...?Still guessing here.
I'll try to explain what this macro should do more clearly... 1. User opens a CATProduct which is let's say, "reference design" for him 2. User needs to save this "reference design" model under new, specific name (by replacing sub-string) under new directory 3. User may not delete or overwrite old "reference design" 4. There is possibility that user would want to only save "reference design" under new directory, without changing any name. I hope it clears some thing up. As for the saving folder - My goal was to choose it by folder browser from the beginning, but I've lack of programming skills for that If anything is unclear I will gladly explain it to you.
Cheers Eine Antwort auf diesen Beitrag verfassen (mit Zitat/Zitat des Beitrags) IP |
joehz Moderator Freiberuflicher Konstrukteur
Beiträge: 1057 Registriert: 25.11.2006 Win7 Pro 64 + Ubuntu + Irix6.5.20 Dell Precision M6600 i7-2960XM 2.7GHz 16GB NVidia Quadro M5010 Catia V5R19 VB6Pro.SP6/VBA 6.5.1053
|
erstellt am: 23. Nov. 2015 15:41 <-- editieren / zitieren --> Unities abgeben: Nur für Sylas
So any and all write-ops have to be to a different dir than the 'Ref Dir'(read dir), which is - by definition - read only. Which begs the question: What is the 'File Delete Routine' for? As for the browse dialog: Check the two vbscript versions, especially the one by Rajendra Khope https://msdn.microsoft.com/en-us/library/windows/desktop/bb774065%28v=vs.85%29.aspx Cheers, Joe ------------------ Inoffizielle Catia Hilfeseite Eine Antwort auf diesen Beitrag verfassen (mit Zitat/Zitat des Beitrags) IP |
joehz Moderator Freiberuflicher Konstrukteur
Beiträge: 1057 Registriert: 25.11.2006 Win7 Pro 64 + Ubuntu + Irix6.5.20 Dell Precision M6600 i7-2960XM 2.7GHz 16GB NVidia Quadro M5010 Catia V5R19 VB6Pro.SP6/VBA 6.5.1053
|
erstellt am: 23. Nov. 2015 22:14 <-- editieren / zitieren --> Unities abgeben: Nur für Sylas
|
Sylas Mitglied
Beiträge: 322 Registriert: 19.11.2012 Dell Precision T3500 Intel Xeon W3550 @ 3,07 GHz 12 GB RAM CATIA V5 R28
|
erstellt am: 23. Nov. 2015 22:23 <-- editieren / zitieren --> Unities abgeben:
Zitat: Which begs the question: What is the 'File Delete Routine' for?
That's a good point! Let me think... I thought about possibility, that macro could do some cleanup in the ref dir, when for example PartName <> FileName. That would be extra funcionality of the code P.S. Still the impact of the name changing of the components in the assembly and saving under new name on the external references links must be checked... I read somewhere, that this would work flawless, when the renaming procedure is run as a CATscript, not as a CATvba prohect. If that is the true, then could you show me how to run CATScript from within VBA? P.P.S. What if user wants only to synchronise PartNames with InstanceNames within ref Dir - could we implement such possibility in our little project? Let's say: when the both Inputtext fields are empty, then synchronise PartNames with InstanceNames ans save within the project root path P.P.P.S when I run the project, I get following message: [Diese Nachricht wurde von Sylas am 23. Nov. 2015 editiert.] [Diese Nachricht wurde von Sylas am 23. Nov. 2015 editiert.] Eine Antwort auf diesen Beitrag verfassen (mit Zitat/Zitat des Beitrags) IP |
joehz Moderator Freiberuflicher Konstrukteur
Beiträge: 1057 Registriert: 25.11.2006 Win7 Pro 64 + Ubuntu + Irix6.5.20 Dell Precision M6600 i7-2960XM 2.7GHz 16GB NVidia Quadro M5010 Catia V5R19 VB6Pro.SP6/VBA 6.5.1053
|
erstellt am: 24. Nov. 2015 00:52 <-- editieren / zitieren --> Unities abgeben: Nur für Sylas
Zitat:
P.S. Still the impact of the name changing of the components in the assembly and saving under new name on the external references links must be checked... I read somewhere, that this would work flawless, when the renaming procedure is run as a CATscript, not as a CATvba prohect. If that is the true, then could you show me how to run CATScript from within VBA?
Test it. Rename the files. Close the product. Reopen the product. Check the links. Zitat:
P.P.S. What if user wants only to synchronise PartNames with InstanceNames within ref Dir - could we implement such possibility in our little project? Let's say: when the both Inputtext fields are empty, then synchronise PartNames with InstanceNames ans save within the project root path
No. Only a different folder. The files are your originals/reference. Design a strategy first and stick to it! That error message looks like VB got messed up. Save. Close catia and reopen again. Cheers, Joe PS: Have you seen my latest post? ------------------ Inoffizielle Catia Hilfeseite Eine Antwort auf diesen Beitrag verfassen (mit Zitat/Zitat des Beitrags) IP |
Sylas Mitglied
Beiträge: 322 Registriert: 19.11.2012 Dell Precision T3500 Intel Xeon W3550 @ 3,07 GHz 12 GB RAM CATIA V5 R28
|
erstellt am: 26. Nov. 2015 07:51 <-- editieren / zitieren --> Unities abgeben:
|
joehz Moderator Freiberuflicher Konstrukteur
Beiträge: 1057 Registriert: 25.11.2006 Win7 Pro 64 + Ubuntu + Irix6.5.20 Dell Precision M6600 i7-2960XM 2.7GHz 16GB NVidia Quadro M5010 Catia V5R19 VB6Pro.SP6/VBA 6.5.1053
|
erstellt am: 26. Nov. 2015 11:36 <-- editieren / zitieren --> Unities abgeben: Nur für Sylas
|
Sylas Mitglied
Beiträge: 322 Registriert: 19.11.2012 Dell Precision T3500 Intel Xeon W3550 @ 3,07 GHz 12 GB RAM CATIA V5 R28
|
erstellt am: 28. Nov. 2015 21:00 <-- editieren / zitieren --> Unities abgeben:
Hi It all works great! Thank you Joe for your help and patience. Only one glitch bothers me... what will happen when the root or goal path will be very long? Will your UserForm self-adopt to the label length? or should I set the width fix somehow? Eine Antwort auf diesen Beitrag verfassen (mit Zitat/Zitat des Beitrags) IP |
joehz Moderator Freiberuflicher Konstrukteur
Beiträge: 1057 Registriert: 25.11.2006 Win7 Pro 64 + Ubuntu + Irix6.5.20 Dell Precision M6600 i7-2960XM 2.7GHz 16GB NVidia Quadro M5010 Catia V5R19 VB6Pro.SP6/VBA 6.5.1053
|
erstellt am: 28. Nov. 2015 21:42 <-- editieren / zitieren --> Unities abgeben: Nur für Sylas
|