/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; using System.IO; //using System.Threading; using System.Xml; using Apache.NMS; using Apache.NMS.Util; using System.Linq; using System.Xml.Linq; using System.Collections.Generic; using Inventor; using Apache.NMS.ActiveMQ.Commands; using System.Text; using System.Xml.Serialization; using System.Diagnostics; using System.Threading; namespace Apache.NMS.ActiveMQ.Test { public class TestMain { protected static AutoResetEvent semaphore = new AutoResetEvent(false); protected static ITextMessage message = null; protected static TimeSpan receiveTimeout = TimeSpan.FromSeconds(10); public static void Main(string[] args) { // Example connection strings: // activemq:tcp://activemqhost:61616 // stomp:tcp://activemqhost:61613 // ems:tcp://tibcohost:7222 // msmq://localhost String inputDir = Directory.GetCurrentDirectory() + @"\" + "Input.properties"; var data = new Dictionary(); foreach (var row in System.IO.File.ReadAllLines(inputDir)) data.Add(row.Split('=')[0], string.Join("=", row.Split('=').Skip(1).ToArray())); String ActiveMQURI = data["ActiveMQURI"]; String outPutPath = data["outPutPath"]; String backupPath = data["backupPath"]; String startQueue = data["startQueue"]; String fileLocation = data["fileLocation"]; String cgrCreationExe = data["cgrCreationExe"]; try { Uri connecturi = new Uri(ActiveMQURI); Console.WriteLine("About to connect to " + connecturi); // NOTE: ensure the nmsprovider-activemq.config file exists in the executable folder. IConnectionFactory factory = new NMSConnectionFactory(connecturi); Inventor.Application InvApp = (Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application"); Inventor.NameValueMap openFileOptions = InvApp.TransientObjects.CreateNameValueMap(); openFileOptions.Add("SkipAllUnresolvedFiles", true); //this is for opening document even if there are unresolved ref. Inventor.TranslatorAddIn STEPAddin = InvApp.ApplicationAddIns.ItemById["{90AF7F40-0C01-11D5-8E83-0010B541CD80}"] as TranslatorAddIn; if (!STEPAddin.Activated) { STEPAddin.Activate(); } InvApp.SilentOperation = true; using (IConnection connection = factory.CreateConnection()) using (ISession session = connection.CreateSession()) { IDestination destination = SessionUtil.GetDestination(session, "queue://STP_CREATION_KO"); IDestination STP_CREATION_OK = SessionUtil.GetDestination(session, "queue://STP_CREATION_OK"); IMessageProducer producer = session.CreateProducer(destination); IMessageProducer producer_OK = session.CreateProducer(STP_CREATION_OK); Console.WriteLine("Using destination: " + destination); // Create a consumer and producer using (IMessageConsumer consumer = session.CreateConsumer(destination)) { connection.Start(); consumer.Listener += new MessageListener(OnMessage); var q = session.GetQueue(startQueue); var b = session.CreateBrowser(q); var msgs = b.GetEnumerator(); while (msgs.MoveNext()) { //this writerKO, xmlKO is for producing Failed logs STP creation String strStpFilesPathInput = ""; StringBuilder xmlKO = new StringBuilder(); XmlWriter writerKO = XmlWriter.Create(xmlKO); //this writerOK, xmlOK is for producing Successful logs STP creation StringBuilder xmlOK = new StringBuilder(); XmlWriter writerOK = XmlWriter.Create(xmlOK); //writing root elements to both xml writerKO.WriteStartElement("ipttoprocess"); writerOK.WriteStartElement("ipttoprocess"); ITextMessage message = msgs.Current as ITextMessage; // Console.WriteLine("message::" + message+"\n"); XmlDocument docRoot = new XmlDocument(); docRoot.LoadXml(message.Text); docRoot.Save(Directory.GetCurrentDirectory() + "//document.xml"); XDocument doc = XDocument.Load(Directory.GetCurrentDirectory() + "//document.xml"); IEnumerable partElements = doc.Root.Elements("ipttoprocesselement"); foreach (XElement partElement in partElements) { // read attribute value string objectId = partElement.Attribute("objectId")?.Value; string type = partElement.Attribute("type")?.Value; string name = partElement.Attribute("name")?.Value; string revision = partElement.Attribute("revision")?.Value; //Console.WriteLine("objectId>>"+ objectId); foreach (XElement partChildElement in partElement.Elements()) { // check the name if (partChildElement.Name == "formatfile") { string fileName = partChildElement.Attribute("name")?.Value; // Console.WriteLine("objectId>>" + objectId+ " fileName::"+ fileName); String folder = type + "_" + name + "_" + revision; String iptPath = fileLocation + @"\" + folder; if (!Directory.Exists(iptPath)) { Console.WriteLine("Path doesn't exists"); continue; } Directory.CreateDirectory(outPutPath + "\\" + folder); Directory.CreateDirectory(backupPath + "\\" + folder); String completefileName = fileLocation + @"\" + type + "_" + name + "_" + revision + @"\" + fileName; // Console.WriteLine(completefileName); if (!System.IO.File.Exists(completefileName)) { continue; } Inventor.Document oDocument = null; try { oDocument = InvApp.ActiveDocument; // oDocument = InvApp.Documents.Open(completefileName, false); oDocument = InvApp.Documents.OpenWithOptions(completefileName, openFileOptions, false); Inventor.TranslationContext oContext = InvApp.TransientObjects.CreateTranslationContext(); oContext.Type = Inventor.IOMechanismEnum.kFileBrowseIOMechanism; Inventor.NameValueMap oOptions = InvApp.TransientObjects.CreateNameValueMap(); Inventor.DataMedium oDataMedium = InvApp.TransientObjects.CreateDataMedium(); if (STEPAddin.HasSaveCopyAsOptions[oDocument, oContext, oOptions]) { oOptions.set_Value("GeometryType", 1); } string NameDocument = oDocument.DisplayName; NameDocument = NameDocument.Substring(0, NameDocument.Length - 4); oDataMedium.FileName = outPutPath + "\\" + folder + "\\" + NameDocument + ".ipt.stp"; STEPAddin.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium); //sConsole.WriteLine("68 completed>>>"); string path = backupPath + "\\" + folder + "\\" + NameDocument + ".ipt.stp"; System.IO.File.Copy(oDataMedium.FileName, path, true); partElement.SetAttributeValue("FilePath", completefileName); partElement.WriteTo(writerOK); strStpFilesPathInput += "\"" + oDataMedium.FileName + "\"" + "?"; } catch (Exception ex) { partElement.SetAttributeValue("FilePath", completefileName); partElement.WriteTo(writerKO); //Console.WriteLine("220::xmlOK exception" + xmlOK.ToString()); } finally { oDocument.Close(); } } } } // Root element - end tag writerKO.WriteEndElement(); // End Documentd writerKO.WriteEndDocument(); // Flush it writerKO.Flush(); if (xmlKO.ToString().Contains("ipttoprocesselement")) { ITextMessage request = session.CreateTextMessage(xmlKO.ToString()); request.NMSCorrelationID = Guid.NewGuid().ToString(); request.NMSReplyTo = destination; producer.Send(request); } writerOK.WriteEndElement(); // End Documentd writerOK.WriteEndDocument(); // Flush it writerOK.Flush(); //this is for producing successful queue - STP_CREATION_OK if (xmlOK.ToString().Contains("ipttoprocesselement")) { ITextMessage requestOK = session.CreateTextMessage(xmlOK.ToString()); requestOK.NMSCorrelationID = Guid.NewGuid().ToString(); requestOK.NMSReplyTo = STP_CREATION_OK; producer_OK.Send(requestOK); } semaphore.WaitOne((int)receiveTimeout.TotalMilliseconds, true); try { // Console.WriteLine("251 ------" + cgrCreationExe + " " + strStpFilesPathInput); LaunchCommandLineApp(cgrCreationExe, strStpFilesPathInput); } catch (Exception ex) { Console.WriteLine("255 ---" + ex.GetBaseException()); } } } } STEPAddin.Deactivate(); InvApp.SilentOperation = false; } catch (Exception ex) { Console.WriteLine(ex.GetBaseException()); } } static void LaunchCommandLineApp(String cgrCreationExe, String strStpFilesPathInput) { try { Process firstProc = new Process(); firstProc.StartInfo.FileName = cgrCreationExe; firstProc.EnableRaisingEvents = true; firstProc.StartInfo.Arguments = strStpFilesPathInput; firstProc.Start(); firstProc.WaitForExit(); } catch (Exception ex) { // Log error. Console.WriteLine("287 ---" + ex.GetBaseException()); } } protected static void OnMessage(IMessage receivedMsg) { message = receivedMsg as ITextMessage; semaphore.Set(); } } }