示例#1
0
        //Rvt导出为Ifc 事件
        private void Uiapp_Idling(object sender, Autodesk.Revit.UI.Events.IdlingEventArgs e)
        {
            if (count != 0)
            {
                return;
            }
            if (File.Exists(rvtName))
            {
                try
                {
                    Document doc = uiapp.Application.OpenDocumentFile(rvtName);
                    using (Transaction transaction = new Transaction(doc, "ifcexporter"))
                    {
                        transaction.Start();
                        IFCExportOptions opt = null;
                        doc.Export(path, ifcName, opt);
                        transaction.Commit();
                    }
                    doc.Close();
                    return;
                }
                catch (Exception ex)
                {
                    TaskDialog.Show("error", ex.ToString());
                }
                finally
                {
                    #region 发送生成的ifc
                    if (File.Exists(rvtName.Replace(".rvt", ".ifc")))
                    {
                        using (var fs = new FileStream(rvtName.Replace(".rvt", ".ifc"), FileMode.Open))
                        {
                            // 封包体
                            byte[] bodyBytes = new byte[fs.Length];
                            fs.Read(bodyBytes, 0, bodyBytes.Length);
                            fs.Close();
                            // 封包头
                            PkgHeader header = new PkgHeader();
                            header.Id       = ++id;
                            header.BodySize = bodyBytes.Length;
                            byte[] headerBytes = server.StructureToByte <PkgHeader>(header);


                            // 组合最终发送的封包 (封包头+封包体)
                            byte[] sendBytes = GetSendBuffer(headerBytes, bodyBytes);
                            server.Send(ID, sendBytes, sendBytes.Length);
                        }
                        delDir(path);//发送完成,删除文件
                    }
                    else
                    {
                        TaskDialog.Show("", rvtName.Replace(".rvt", ".ifc"));
                    }
                    #endregion
                }
                count++;
            }
        }
示例#2
0
        private HandleResult Server_OnReceive(IntPtr connId, int length)
        {
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            #region 收数据
            // 需要长度
            int required = pkgInfo.Length;

            // 剩余大小
            int remain = length;

            while (remain >= required)
            {
                IntPtr bufferPtr = IntPtr.Zero;
                try
                {
                    remain   -= required;
                    bufferPtr = Marshal.AllocHGlobal(required);
                    if (server.Fetch(connId, bufferPtr, required) == FetchResult.Ok)
                    {
                        if (pkgInfo.IsHeader == true)
                        {
                            PkgHeader header = (PkgHeader)Marshal.PtrToStructure(bufferPtr, typeof(PkgHeader));

                            required = header.BodySize;
                        }
                        else
                        {
                            //intptr转byte[]
                            byte[] bytes = new byte[required];
                            Marshal.Copy(bufferPtr, bytes, 0, required);
                            using (var fs = new FileStream(rvtName, FileMode.Create))
                            {
                                fs.Write(bytes, 0, bytes.Length);
                                fs.Close();
                            }

                            required = pkgHeaderSize;
                        }

                        // 在后面赋值,因为前面需要用到pkgInfo.Length
                        pkgInfo.IsHeader = !pkgInfo.IsHeader;
                        pkgInfo.Length   = required;
                        if (server.SetExtra(connId, pkgInfo) == false)
                        {
                            return(HandleResult.Error);
                        }
                    }
                }
                catch
                {
                    return(HandleResult.Error);
                }
                finally
                {
                    if (bufferPtr != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(bufferPtr);
                        bufferPtr = IntPtr.Zero;
                    }
                }
            }
            #endregion

            return(HandleResult.Ok);
        }