예제 #1
0
파일: Model.cs 프로젝트: gaodansoft/sw
        private IDocumentSpecification GetSpec()
        {
            IDocumentSpecification spec = (IDocumentSpecification)iSwApp.GetOpenDocSpec(modelName);

            spec.LoadModel = true;
            return(spec);
        }
예제 #2
0
        /// <summary>
        /// Open a document invisibly. It will not be shown to the user but you will be
        /// able to interact with it through the API as if it is loaded.
        /// </summary>
        /// <param name="sldWorks"></param>
        /// <param name="toolFile"></param>
        /// <returns></returns>
        public static ModelDoc2 OpenInvisibleReadOnly(this ISldWorks sldWorks, string toolFile, bool visible = false, swDocumentTypes_e type = swDocumentTypes_e.swDocPART)
        {
            try
            {
                if (!visible)
                {
                    sldWorks.DocumentVisible(false, (int)type);
                }
                var spec = (IDocumentSpecification)sldWorks.GetOpenDocSpec(toolFile);
                if (!visible)
                {
                    spec.Silent   = true;
                    spec.ReadOnly = true;
                }
                var doc = SwAddinBase.Active.SwApp.OpenDoc7(spec);

                doc.Visible = visible;
                return(doc);
            }
            finally
            {
                if (!visible)
                {
                    sldWorks.DocumentVisible
                        (true,
                        (int)
                        type);
                }
            }
        }
        protected IDisposable OpenDataDocument(string name, bool readOnly = true, Action <IDocumentSpecification> specEditor = null)
        {
            var filePath = GetFilePath(name);

            var spec = (IDocumentSpecification)m_SwApp.GetOpenDocSpec(filePath);

            spec.ReadOnly    = readOnly;
            spec.LightWeight = false;
            specEditor?.Invoke(spec);

            var model = m_SwApp.OpenDoc7(spec);

            if (model != null)
            {
                if (model is IAssemblyDoc)
                {
                    (model as IAssemblyDoc).ResolveAllLightWeightComponents(false);
                }

                var docWrapper = new DocumentWrapper(m_SwApp, model);
                m_Disposables.Add(docWrapper);
                return(docWrapper);
            }
            else
            {
                throw new NullReferenceException($"Failed to open the the data document at '{filePath}'");
            }
        }
예제 #4
0
    public ModelDoc2 OpenSwAssembly(string path)
    {
        ModelDoc2             swModel            = default(ModelDoc2);
        DocumentSpecification swDocSpecification = default(DocumentSpecification);

        string[] componentsArray = new string[1];
        object[] components      = null;
        string   name            = null;
        int      errors          = 0;
        int      warnings        = 0;

        //Set the specifications
        swDocSpecification = (DocumentSpecification)app.GetOpenDocSpec(path);

        componentsArray[0] = "food bowl-1@bowl and chute";
        components         = (object[])componentsArray;

        swDocSpecification.ComponentList = components;
        swDocSpecification.Selective     = true;
        name = swDocSpecification.FileName;

        swDocSpecification.DocumentType          = (int)swDocumentTypes_e.swDocASSEMBLY;
        swDocSpecification.DisplayState          = "Default_Display State-1";
        swDocSpecification.UseLightWeightDefault = false;
        swDocSpecification.LightWeight           = true;
        swDocSpecification.Silent = true;
        swDocSpecification.IgnoreHiddenComponents = true;

        //Open the assembly document as per the specifications
        swModel  = (ModelDoc2)app.OpenDoc7(swDocSpecification);
        errors   = swDocSpecification.Error;
        warnings = swDocSpecification.Warning;

        Debugger.Break();

        if (errors > 0)
        {
            throw new Exception("Error while opening Solidworks assembly");
        }


        if (warnings > 0)
        {
            System.Diagnostics.Debug.WriteLine("Warning while opening Soliworks assembly");
        }

        activeModel = swModel;

        return(swModel);
    }
예제 #5
0
        public static DocumentSpecification GetDocumentSpecification(
            ISldWorks swApp, string path)
        {
            try
            {
                try
                {
                    if (path == null ||
                        string.Compare(path, "") == 0 ||
                        swApp == null)
                    {
                        throw new ArgumentException();
                    }
                }
                catch (ArgumentException)
                {
                    return(null);
                }

                try
                {
                    if (!File.Exists(path))
                    {
                        throw new ArgumentException();
                    }
                }
                catch (ArgumentException)
                {
                    return(null);
                }

                DocumentSpecification documentSpecification =
                    (DocumentSpecification)swApp.GetOpenDocSpec(path);

                if (documentSpecification == null)
                {
                    throw new Exception();
                }

                return(documentSpecification);
            } catch (Exception)
            {
                return(null);
            }
        }
예제 #6
0
        private void UpdateCachedBodyIfNeeded(LinkFileMacroFeatureParameters parameters)
        {
            LastError = null;
            IModelDoc2 refDoc         = null;
            bool       isRefDocLoaded = false;

            try
            {
                if (File.Exists(parameters.LinkedFilePath))
                {
                    LastUpdateStamp = File.GetLastWriteTimeUtc(parameters.LinkedFilePath).Ticks;

                    refDoc = m_App.GetOpenDocumentByName(parameters.LinkedFilePath) as IModelDoc2;

                    isRefDocLoaded = refDoc != null;

                    if (LastUpdateStamp != parameters.FileLastUpdateTimeStamp ||
                        (isRefDocLoaded && refDoc.GetSaveFlag()) || CachedBodies == null)
                    {
                        if (!isRefDocLoaded)
                        {
                            m_App.DocumentVisible(false, (int)swDocumentTypes_e.swDocPART);

                            var docSpec = m_App.GetOpenDocSpec(parameters.LinkedFilePath) as IDocumentSpecification;
                            docSpec.Silent   = true;
                            docSpec.ReadOnly = true;

                            refDoc = m_App.OpenDoc7(docSpec);

                            if (refDoc == null)
                            {
                                throw new InvalidOperationException($"Failed to load the referenced file ${docSpec.FileName} with error: {(swFileLoadError_e)docSpec.Error}");
                            }
                        }

                        if (refDoc is IPartDoc)
                        {
                            var bodies = (refDoc as IPartDoc).GetBodies2((int)swBodyType_e.swAllBodies, true) as object[];

                            if (bodies != null && bodies.Any())
                            {
                                var resBodies = bodies.Cast <IBody2>().Select(b => b.ICopy()).ToArray();

                                CachedBodies = resBodies;
                            }
                            else
                            {
                                throw new InvalidOperationException("No bodies in the referenced document");
                            }
                        }
                        else
                        {
                            throw new InvalidOperationException("Referenced document is not a part");
                        }
                    }
                }
                else
                {
                    throw new FileNotFoundException($"Linked file '${parameters.LinkedFilePath}' is not found");
                }
            }
            catch (Exception ex)
            {
                LastError = ex;
            }
            finally
            {
                m_App.DocumentVisible(true, (int)swDocumentTypes_e.swDocPART);

                if (!isRefDocLoaded && refDoc != null)
                {
                    m_App.CloseDoc(refDoc.GetTitle());
                }
            }
        }
예제 #7
0
        public static DocumentSpecification GetDocumentSpecification(
            ISldWorks swApp, string path)
        {
            logger.Debug("\n Getting Document Specification for File: " +
                         path);
            try
            {
                try
                {
                    if (path == null ||
                        string.Compare(path, "") == 0 ||
                        swApp == null)
                    {
                        throw new ArgumentException();
                    }
                }
                catch (ArgumentException exception)
                {
                    logger.Error(exception, "\n ERROR: Document Specification path or SolidWorks app reference was null or empty");

                    return(null);
                }

                try
                {
                    logger.Debug("\n Checking file " + path + " exists");

                    if (!File.Exists(path))
                    {
                        throw new ArgumentException();
                    }

                    logger.Debug("\n File " + path + " Found");
                }
                catch (ArgumentException exception)
                {
                    logger.Error(exception, "\n ERROR: File " + path + " Not Found");

                    return(null);
                }

                DocumentSpecification documentSpecification =
                    (DocumentSpecification)swApp.GetOpenDocSpec(path);

                logger.Debug("\n Returning Document Specification for " + path +
                             "\n " + documentSpecification);

                if (documentSpecification == null)
                {
                    throw new Exception();
                }

                return(documentSpecification);
            } catch (Exception exception)
            {
                logger.Error(exception, "\n ERROR: Could Not Get Document Specification For " +
                             path);

                return(null);
            }
        }
예제 #8
0
        /// <summary>
        /// Экспорт свойств компонентов в базу данных
        /// </summary>         
        public void AttributesExport(ISldWorks swApp)
        {
            try
            {
                Connection = new OracleConnection(ConnectionString);
                Connection.Open();

                string orderPath = (((ModelDoc2)swApp.ActiveDoc)).GetPathName();
                string orderNum = Path.GetFileName(Path.GetDirectoryName(orderPath));

                //очистка данных по заказу

                using (OracleCommand cmd = new OracleCommand())
                {
                    cmd.Connection = Connection;
                    cmd.CommandText = "GENERAL.SWR_FILEDATA_CLEAR_SW";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("P_NUMAGREE", OracleDbType.Varchar2).Value = orderNum;
                    cmd.Parameters.Add("P_COMMIT", OracleDbType.Char).Value = 'T';

                    cmd.ExecuteNonQuery();
                }

                //Коллекция уникальных сочетаний компонент-свойство
                //Dictionary<KeyValuePair<string, string>, string> componentsAttributesDictionary = new Dictionary<KeyValuePair<string, string>, string>();
                Collection<AttributeInfo> componentsAttributesCollection = new Collection<AttributeInfo>();

                Action<ModelDoc2> action =
                    new Action<ModelDoc2>((model) =>
                    {
                        string path = model.GetPathName();
                        DocumentSpecification swDocSpecification = (DocumentSpecification)swApp.GetOpenDocSpec(path);
                        int fileType = swDocSpecification.DocumentType;
                        string filePath = path.Substring(0, path.LastIndexOf(@"\") + 1);
                        string fileName = Path.GetFileName(Path.GetFileName(path));

                        string configuration = string.Empty;
                        sendAttributes(model, componentsAttributesCollection, path, orderNum, fileType, filePath, fileName, configuration);
                        configuration = model.IGetActiveConfiguration().Name;
                        sendAttributes(model, componentsAttributesCollection, path, orderNum, fileType, filePath, fileName, configuration);
                    });

                SolidWorksInterop.DoSmthForEachComponent((AssemblyDoc)swApp.ActiveDoc, action);
            }
            finally
            {
                Connection.Close();
            }
        }