using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using INFITF;
using ProductStructureTypeLib;
using MECMOD;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Windows.Automation;
using log4net;
using log4net.Config;
//using System.Windows.Forms.Application
namespace CleanHarness
{
class Program
{
static private string CATIAV5APPNAME = "CATIA V5";
static private string DELUSELESSELEMENT = "Delete Useless Elements";
static private string OK = "OK";
private static readonly log4net.ILog log = log4net.LogManager.GetLogger
(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
static void Main(string[] args)
{
//TODO:Can't get my logger to work
log.Info("This is a Info message");
INFITF.Application CATIA = (INFITF.Application)Marshal.GetActiveObject("Catia.Application");
// Set references to Catia
Document pActiveDoc = CATIA.ActiveDocument;
//Retrieve the Selection object
Selection pSel = CATIA.ActiveDocument.Selection;
//Dim isPart As Boolean = IsCatiaPart(pSel.Item(1).Value)
// Selection test:
// The Harness doc success the cast in Part
// The Harness part fail the cast in Part and success for Prod
// The split harness product success cast in Prod
// Did the user select the document?
try
{
//Get the object in the selection
Part pPart = (Part)pSel.Item2(1).Value;
//Display message
Console.WriteLine("Selected element: " + pPart.get_Name());
pSel.Clear();
pSel.Add(pPart);
DeleteUselessElements(CATIA);
}
catch
{
try
{
Console.WriteLine("Selection is not the document.");
//Did the user select the product?
//Can//t cast in ProductDocument need to be cast in Product.
Product pProd = (Product)pSel.Item(1).Value;
Console.WriteLine("Success cast to product.");
//Can//t do that directl, need to get the part document!
//DeleteUselessElements(CATIA)
//Dim isPart As Boolean = IsCatiaPart(pSel.Item(1).Value)
//Console.WriteLine("Is this a part:" + isPart)
}
catch
{
Console.WriteLine("Not a product.");
}
}
//Console.WriteLine("Is Part:" + isPart.ToString)
Console.ReadLine();
}
//Call Catia command: Delete Useless Elements and click "ok" button
static public void DeleteUselessElements(INFITF.Application CATIA)
{
Console.WriteLine("Deleting Useless Elements on selected document.");
CATIA.StartCommand("Delete Useless Elements");
AutomationElementCollection childs =
AutomationElement.RootElement.FindAll(
TreeScope.Children, Condition.TrueCondition);
AutomationElement catiaWindow = null;
foreach (AutomationElement child in childs)
{
//Debug
Console.WriteLine(child.Current.Name);
string appName = child.Current.Name;
if (appName.Contains(CATIAV5APPNAME))
{
catiaWindow = child;
}
}
if(catiaWindow == null)
{
Console.WriteLine("Could not found application named:" + CATIAV5APPNAME);
return;
}
Condition graphWinCond =
new PropertyCondition(AutomationElement.NameProperty, DELUSELESSELEMENT);
AutomationElement graphWin;
//wait for Graph window to open and get it
do
{
graphWin = catiaWindow.FindFirst(TreeScope.Children, graphWinCond);
System.Windows.Forms.Application.DoEvents();
} while (graphWin == null);
Condition btnOKCondition =
new PropertyCondition(AutomationElement.NameProperty, OK);
AutomationElement btnOk =
graphWin.FindFirst(TreeScope.Children, btnOKCondition);
//control pattern definition (button click)
InvokePattern patOK = null;
btnOk.GetCurrentPattern(InvokePattern.Pattern);
try
{
patOK = btnOk.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
}
catch (InvalidOperationException ex)
{
Console.WriteLine(ex.Message); // Most likely "Pattern not supported."
return;
}
//Click Ok button
patOK.Invoke();
}
}
}