Exemplo n.º 1
0
        public bool ConnectToOpenCOVER(string host, int port, Autodesk.Revit.DB.Document doc)
        {
            document = doc;
             handler = new externalMessageHandler();
             messageEvent = Autodesk.Revit.UI.ExternalEvent.Create(handler);
             messageQueue = new Queue<COVERMessage>();

             System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName("Revit");

             if (0 < processes.Length)
             {
             ApplicationWindow = processes[0].MainWindowHandle;
             }
             try
             {
             if (toCOVER != null)
             {
                 messageThread.Abort(); // stop reading from the old toCOVER connection
                 toCOVER.Close();
                 toCOVER = null;
                 setConnected(false);
             }

             toCOVER = new TcpClient(host, port);
             if (toCOVER.Connected)
             {
                 // Sends data immediately upon calling NetworkStream.Write.
                 toCOVER.NoDelay = true;
                 LingerOption lingerOption = new LingerOption(false, 0);
                 toCOVER.LingerState = lingerOption;

                 NetworkStream s = toCOVER.GetStream();
                 Byte[] data = new Byte[256];
                 data[0] = 1;
                 try
                 {
                     //toCOVER.ReceiveTimeout = 1000;
                     s.Write(data, 0, 1);
                     //toCOVER.ReceiveTimeout = 10000;
                 }
                 catch (System.IO.IOException)
                 {
                     // probably socket closed
                     setConnected(false);
                     return false;
                 }

                 int numRead = 0;
                 try
                 {
                     //toCOVER.ReceiveTimeout = 1000;
                     numRead = s.Read(data, 0, 1);
                     //toCOVER.ReceiveTimeout = 10000;
                 }
                 catch (System.IO.IOException)
                 {
                     // probably socket closed
                     setConnected(false);
                     return false;
                 }

                 List<View3D> views = new List<View3D>(
             new FilteredElementCollector(doc)
               .OfClass(typeof(View3D))
               .Cast<View3D>()
               .Where<View3D>(v =>
             v.CanBePrinted && !v.IsTemplate));
                 int n = views.Count;

                 if (0 == n)
                 {
                     setConnected(false);
                     return false;
                 }
                 MessageBuffer mb = new MessageBuffer();
                 mb.add(n);
                 foreach (View3D v in views)
                 {
                     mb.add(v.Name);
                 }
                 sendMessage(mb.buf, MessageTypes.Views);

                 if (numRead == 1)
                 {
                     setConnected(true);
                     messageThread = new Thread(new ThreadStart(this.handleMessages));

                     // Start the thread
                     messageThread.Start();

                 }

                 return true;
             }
             System.Windows.Forms.MessageBox.Show("Could not connect to OpenCOVER on localhost, port 31821");
             }
             catch
             {

             setConnected(false);
             System.Windows.Forms.MessageBox.Show("Connection error while trying to connect to OpenCOVER on localhost, port 31821");
             }
             return false;
        }
Exemplo n.º 2
0
Arquivo: COVER.cs Projeto: nixz/covise
        public bool ConnectToOpenCOVER(string host, int port, Autodesk.Revit.DB.Document doc)
        {
            document = doc;
             handler = new externalMessageHandler();
             messageEvent = Autodesk.Revit.UI.ExternalEvent.Create(handler);
             messageQueue = new Queue<COVERMessage>();

             System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName("Revit");
             callCounter = 0;

             if (0 < processes.Length)
             {
             ApplicationWindow = processes[0].MainWindowHandle;
             }
             try
             {
             if (toCOVER != null)
             {
                 messageThread.Abort(); // stop reading from the old toCOVER connection
                 toCOVER.Close();
                 toCOVER = null;
             }

             toCOVER = new TcpClient(host, port);
             if (toCOVER.Connected)
             {
                 // Sends data immediately upon calling NetworkStream.Write.
                 toCOVER.NoDelay = true;
                 LingerOption lingerOption = new LingerOption(false, 0);
                 toCOVER.LingerState = lingerOption;

                 NetworkStream s = toCOVER.GetStream();
                 Byte[] data = new Byte[256];
                 data[0] = 1;
                 try
                 {
                     //toCOVER.ReceiveTimeout = 1000;
                     s.Write(data, 0, 1);
                     //toCOVER.ReceiveTimeout = 10000;
                 }
                 catch (System.IO.IOException e)
                 {
                     // probably socket closed
                     return false;
                 }

                 int numRead = 0;
                 try
                 {
                     //toCOVER.ReceiveTimeout = 1000;
                     numRead = s.Read(data, 0, 1);
                     //toCOVER.ReceiveTimeout = 10000;
                 }
                 catch (System.IO.IOException e)
                 {
                     // probably socket closed
                     return false;
                 }
                 if (numRead == 1)
                 {

                     messageThread = new Thread(new ThreadStart(this.handleMessages));

                     // Start the thread
                     messageThread.Start();
                 }

                 return true;
             }
             System.Windows.Forms.MessageBox.Show("Could not connect to OpenCOVER on localhost, port 31821");
             }
             catch
             {
             System.Windows.Forms.MessageBox.Show("Connection error while trying to connect to OpenCOVER on localhost, port 31821");
             }
             return false;
        }