Example #1
0
        public void SaveCgrToProject(string docName)
        {
            Documents       docs    = myCATIA.Documents;
            ProductDocument prodDoc = (ProductDocument)docs.Add("Product");
            Product         prod    = prodDoc.Product;
            Products        prods   = prod.Products;
            Array           arr     = Array.CreateInstance(typeof(object), 1);

            arr.SetValue(docName + ".cgr", 0);
            prods.AddComponentsFromFiles(arr, "All");

            string dir  = docName.Substring(0, docName.LastIndexOf('\\') + 1);
            string name = docName.Substring(docName.LastIndexOf('\\') + 1);

            docName = dir + RemoveDiacritics(name);
            prodDoc = (ProductDocument)myCATIA.ActiveDocument;
            if (System.IO.File.Exists(docName + ".CATProduct"))
            {
                System.IO.File.Delete(docName + ".CATProduct");
            }
            try
            {
                prodDoc.SaveAs(docName + ".CATProduct");
            }
            catch (Exception)
            {
                Console.WriteLine("CGR document '" + docName +
                                  "' could not be saved as CATProduct.");
            }
            if (prodDoc != null)
            {
                prodDoc.Close();
            }
        }
Example #2
0
        public string ExportToCgr(string path, string dir)
        {
            string dirName = path.Substring(0, path.LastIndexOf('\\'));

            dirName = dirName.Substring(0, dirName.LastIndexOf('\\'));
            dirName = dirName.Substring(dirName.LastIndexOf('\\') + 1);
            string name = path.Substring(path.LastIndexOf('\\') + 1);

            name = name.Substring(0, name.LastIndexOf('.'));

            ProductDocument prodDoc = null;

            try
            {
                prodDoc = (ProductDocument)myCATIA.Documents.Read(path);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Path '" + path + "' could not be read.");
                String errType = ex.GetType().Name;
                if (errType == "TimeoutException")
                {
                    //TimeoutException e = ex as TimeoutException;
                    Console.WriteLine("It's the TimeoutException !");
                }
                if (errType == "COMException")
                {
                    COMException COMex = ex as COMException;
                    Console.WriteLine("COMerror " + COMex.ErrorCode.ToString() + ": " + COMex.Message);
                }
                if (prodDoc != null)
                {
                    prodDoc.Close();
                }
                return(null);
            }

            string prodDocName = dir + "\\" + dirName + " - " + name;

            if (System.IO.File.Exists(prodDocName + ".cgr"))
            {
                System.IO.File.Delete(prodDocName + ".cgr");
            }
            try
            {
                prodDoc.ExportData(prodDocName + ".CGR", "cgr");
            }
            catch (Exception)
            {
                Console.WriteLine("Path '" + path + "' could not be exported.");
                if (prodDoc != null)
                {
                    prodDoc.Close();
                }
                return(null);
            }

            if (prodDoc != null)
            {
                prodDoc.Close();
            }
            return(prodDocName);
        }