Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

CATIA macros Save only active document

Status
Not open for further replies.

elmundo777

Automotive
Jun 23, 2020
93
BY
Hello. I'm want to write a macro (catscript\vb\c#) to save ONLY active document.
But I don't know how to do this.
In this C# code I can save all opened documents
How I can save only active document or last created document?
Code:
            var path = tb_path.Text;
for (int iDoc = 1; iDoc <= CATIA.Documents.Count; iDoc++)
            {
                Document docc = null;
                docc = CATIA.Documents.Item(iDoc);

                string nn = CATIA.Documents.Item(iDoc).get_Name();
                
                string TOTAL = path + @"\" + nn;
                
                docc.SaveAs(TOTAL);
               
            

            }
 
Replies continue below

Recommended for you

Little Cthulhu said:
CATIA.ActiveDocument.SaveAs(path)
This is good idea, but it saves only products and parts from 1 lvl of the tree:
Product (root product)
-Product 1 /save
--Product x /don't save
-Product 2 /save
--Product xx /don't save
---Product xxx /don't save
-Part 1 /save
save_error_fd1g8x.png
 
I'm want to write a macro (catscript\vb\c#) to save ONLY active document.

You asked for it.

To save all products in active documents:

Code:
Dim sel: set sel = CATIA.ActiveDocument.Selection
sel.Search "CATAsmSearch.Product,all"
Dim i: for i=1 to sel.Count
  sel.Item(i).LeafProduct.ReferenceProduct.Parent.SaveAs("path")
next
 
Little Cthulhu said:
You asked for it.

To save all products in active documents:

CODE
Dim sel: set sel = CATIA.ActiveDocument.Selection
sel.Search "CATAsmSearch.Product,all"
Dim i: for i=1 to sel.Count
sel.Item(i).LeafProduct.ReferenceProduct.Parent.SaveAs("path")
I'm have an error here:
er_j210fg.png
 
please check your V5Automation.chm file in your [CATIA install path]code/bin folder. and read about SelectedElement and LeafProduct... that could help you understand how to make it work.

Eric N.
indocti discant et ament meminisse periti
 
itsmyjob said:
please check your V5Automation.chm file in your [CATIA install path]code/bin folder. and read about SelectedElement and LeafProduct... that could help you understand how to make it work.
I have read this document, but it has not become clearer.
Moreover, it saves not with the names of the product / desk, but with the names CATIASelectedElement27 and etc.
cat_kdjfmh.png
 
Did you tried .Value? Or .Name? Or .PartNumber? What you will obtain if you do a manual selection in the tree and what if you select in the graphic area (just for testing purposes)?
This is how I learned, reading and trying different things...
And by the way, you can even find all the unsaved parts in the CATIA.Documents.

Regards
Fernando

 
ferdo said:
Did you tried .Value? Or .Name? Or .PartNumber? What you will obtain if you do a manual selection in the tree and what if you select in the graphic area (just for testing purposes)?
This is how I learned, reading and trying different things...
And by the way, you can even find all the unsaved parts in the CATIA.Documents.
I tried all methods. And this code works for me, but sometimes create empty files.

Code:
Dim sel: set sel = CATIA.ActiveDocument.Selection
sel.Search "CATAsmSearch.Product,all"
Dim i: for i=1 to sel.Count[b]2[/b]
PN = sel.Item(i).Name
path = "H:\!TEST\"
  sel.Item(i).LeafProduct.ReferenceProduct.Parent.SaveAs(path & "\" &  sel.Item(i).Value.[b]PartNumber[/b])     
next
Problem, that i write code in c#.And after LeafProduct visual studio displays an errors:
err_fgo7fb.png
 
ferdo said:
Did you check if you are in design mode?
Error with ReferenceProduct error i am getting in development environment of Visual studio.
Perhaps I should declare links or dependencies somewhere so that there is no error after the ReferenceProduct?
 
Little Cthulhu said:
Cast .LeafProduct to Product.
Cast .Value to AnyObject.
Sorry, but what do you mean by this? Do you have examples of implementation?(vbscript or catscript)
Because in С# I can't seem to find a way to solve this problem
 
Do you know what "cast" in C# is?

var prd = ((Product)....LeafProduct).ReferenceProduct;
var doc = (ProductDocument)prd.Parent;
doc.SaveAs("path");
 
Little Chtulhu said:
Do you know what "cast" in C# is?

var prd = ((Product)....LeafProduct).ReferenceProduct;
var doc = (ProductDocument)prd.Parent;
doc.SaveAs("path");
In "...." did you mean the root product? I'm already confused and don't understand.
I've tried the selection option so far.
And when I'm try this code, I've get an error: Error Hresult E_fail ... COM component.
Code:
 var prd = ((Product)CATIA.ActiveDocument.Selection.Item(i).LeafProduct).ReferenceProduct;
  var doc = (ProductDocument)prd.Parent;
  doc.SaveAs(fullpath);
But products of first level are saved .[hammer]
 
This code works good, but don't save parts, that were added as Existing component[mad]
Code:
 var prd = ((Product)sel.Item2(1).LeafProduct).ReferenceProduct;   
            var doc = (ProductDocument)prd.Parent;
            doc.SaveAs(fullpath);
 
When I'm try to do this in cycle: System.Runtime.InteropServices.COMException: 'Error HRESULT E_FAIL has been returned from a call to a COM component.'
Code:
var sel = CATIA.ActiveDocument.Selection;
sel.Search("CATAsmSearch.Product,all");
            
for (var i = 1; i <= sel.Count; i++)
{
var prd = ((Product)sel.Item2(i).LeafProduct).ReferenceProduct;   
var doc = (ProductDocument)prd.Parent;
doc.SaveAs(TOTAL);
}
 
Why are you using the same name (TOTAL) for all files? CATIA won't allow you to overwrite existing file unless CATIA.DisplayFileAlerts is set to false.
 

Little Cthulhu said:
Why are you using the same name (TOTAL) for all files? CATIA won't allow you to overwrite existing file unless CATIA.DisplayFileAlerts is set to false.
The TOTAL variable consists of the save path variable(true_path) and the name of the active document(nn):
Code:
 var path = tb_path.Text;
 var true_path = path.Replace("/", "\"");
 path.Replace("//", "\\");
var nn = CATIA.ActiveDocument.get_Name();
var TOTAL = true_path + @"\" + nn;

How can I correctly specify in the code to save the entire content of the document?
I really don't understand and have already re-read hundreds of topics on various forums and tried even more code options...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top