Ejemplo n.º 1
0
        internal static HelperResult ImportProperties(SldWorks swApp, BindingSource bindingSource,
                                                      string path, string configName = "")
        {
            Console.WriteLine("Import: path={0}, config={1}", path, configName);


            if (swApp == null)
            {
                return(HelperResult.SLDWORKS_NOT_RUNNING);
            }

            string            ext = Path.GetExtension(path);
            swDocumentTypes_e type;

            if (ext.ToLower().Contains("sldprt"))
            {
                type = swDocumentTypes_e.swDocPART;
            }
            else if (ext.ToLower().Contains("sldasm"))
            {
                type = swDocumentTypes_e.swDocASSEMBLY;
            }
            else if (ext.ToLower().Contains("slddrw") && string.IsNullOrEmpty(configName))
            {
                type = swDocumentTypes_e.swDocDRAWING;
            }
            else
            {
                return(HelperResult.WRONG_ARG);
            }


            int       Error   = 0;
            int       Warning = 0;
            ModelDoc2 doc;


            // checking if the document is open
            if (((ModelDoc2)swApp.ActiveDoc) != null && ((ModelDoc2)swApp.ActiveDoc).GetPathName().ToLower() == path.ToLower())
            {
                doc = (ModelDoc2)swApp.ActiveDoc;
            }
            else
            {
                doc = swApp.OpenDoc6(path, (int)type, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, configName, ref Error, ref Warning);
            }

            if (Error != 0)
            {
                return(HelperResult.OPEN_ERROR);
            }

            CustomPropertyManager manager = doc.Extension.CustomPropertyManager[configName];

            if (manager == null)
            {
                return(HelperResult.OPEN_ERROR);
            }

            string[] names = manager.GetNames();

            if (names == null || !names.Any())
            {
                return(HelperResult.NOT_EXIST);
            }

            BindingList <PropertyObject> list = new BindingList <PropertyObject>();

            foreach (string field in names)
            {
                PropertyObject obj = new PropertyObject();

                obj.FieldName = field;
                int ret = manager.Get6(field, true, out string ValOut, out string ResolvedValOut, out bool WasResolved, out bool LinkToProperty);

                if (ret == (int)swCustomInfoGetResult_e.swCustomInfoGetResult_NotPresent)
                {
                    continue;
                }

                obj.Value = ValOut;
                list.Add(obj);
            }

            if (!list.Any())
            {
                return(HelperResult.NOT_EXIST);
            }

            bindingSource.Clear();
            bindingSource.DataSource = list;

            return(HelperResult.SUCCESS);
        }
Ejemplo n.º 2
0
        internal static HelperResult OpenProperty(BindingSource bindingSource, string path)
        {
            XmlDocument doc = new XmlDocument();

            BindingList <PropertyObject> list = new BindingList <PropertyObject>();



            doc.Load(path);
            //Console.WriteLine("Loaded xml");

            XmlElement el = doc.DocumentElement;

            //Console.WriteLine("elem:" + el.Name);

            XmlNodeList elemList = doc.GetElementsByTagName("property");

            if (elemList.Count == 0)
            {
                return(HelperResult.UNKNOWN);
            }

            for (int i = 0; i < elemList.Count; i++)
            {
                PropertyObject obj = new PropertyObject();

                if (!elemList[i].HasChildNodes)
                {
                    continue;
                }
                obj.FieldName = elemList[i].ChildNodes[0].InnerText;
                if (string.IsNullOrEmpty(obj.FieldName))
                {
                    continue;
                }

                if (elemList[i].ChildNodes[1].InnerText.ToLower() == "bool")
                {
                    obj.Type_Data = TypeData.YesOrNo;
                }
                else if (elemList[i].ChildNodes[1].InnerText.ToLower() == "date")
                {
                    obj.Type_Data = TypeData.Date;
                }
                else if (elemList[i].ChildNodes[1].InnerText.ToLower() == "num")
                {
                    obj.Type_Data = TypeData.Num;
                }
                else
                {
                    obj.Type_Data = TypeData.Text;
                }

                obj.Value = elemList[i].ChildNodes[2].InnerText;

                list.Add(obj);
            }

            bindingSource.DataSource = list;

            return(HelperResult.SUCCESS);
        }