コード例 #1
0
        /// <summary>
        /// 静默模式打开隐藏文件
        /// </summary>
        /// <param name="app"></param>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static ModelDoc2 OpenInvisibleDocClient(this SldWorks app, string filePath, bool Hidden = true)
        {
            int Errors = -1, Warning = -1;
            var type = app.FileType(filePath);

            try
            {
                if (Hidden)
                {
                    app.DocumentVisible(false, (int)type);
                }

                ModelDoc2 doc = app.OpenDoc6(filePath, type.SWToInt(), swOpenDocOptions_e.swOpenDocOptions_Silent.SWToInt(),
                                             "", ref Errors, ref Warning) as ModelDoc2;

                if (doc == null)
                {
                    throw new Exception(string.Format("errors:{0},warings{1}",
                                                      Errors.CastObj <swFileLoadError_e>().ToString(),
                                                      Warning.CastObj <swFileLoadWarning_e>().ToString()));
                }

                return(doc);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                app.DocumentVisible(true, (int)type);
            }
        }
コード例 #2
0
        private static void InitializeSolidWorks()
        {
            string              monikerName = "SolidWorks_PID_";
            object              app;
            IBindCtx            context  = null;
            IRunningObjectTable rot      = null;
            IEnumMoniker        monikers = null;

            try
            {
                CreateBindCtx(0, out context);

                context.GetRunningObjectTable(out rot);
                rot.EnumRunning(out monikers);

                IMoniker[] moniker = new IMoniker[1];

                while (monikers.Next(1, moniker, IntPtr.Zero) == 0)
                {
                    var curMoniker = moniker.First();

                    string name = null;

                    if (curMoniker != null)
                    {
                        try
                        {
                            curMoniker.GetDisplayName(context, null, out name);
                        }
                        catch (UnauthorizedAccessException ex)
                        {
                            MessageObserver.Instance.SetMessage("Failed to get SolidWorks_PID." + "\t" + ex, MessageType.Error);
                            System.Windows.Forms.MessageBox.Show(ex.Message);
                        }
                    }
                    if (name.Contains(monikerName))
                    {
                        rot.GetObject(curMoniker, out app);
                        swApp         = (SldWorks)app;
                        swApp.Visible = true;
                        swApp.DocumentVisible(false, 2);
                        swApp.DocumentVisible(false, 1);
                        return;
                    }
                }
                string progId = "SldWorks.Application";

                Type progType = Type.GetTypeFromProgID(progId);
                app           = Activator.CreateInstance(progType) as SldWorks;
                swApp         = (SldWorks)app;
                swApp.Visible = true;
                //swApp.DocumentVisible(false, 2);
                //swApp.DocumentVisible(false, 1);
                return;
            }
            finally
            {
                if (monikers != null)
                {
                    Marshal.ReleaseComObject(monikers);
                }
                if (rot != null)
                {
                    Marshal.ReleaseComObject(rot);
                }
                if (context != null)
                {
                    Marshal.ReleaseComObject(context);
                }
            }
        }