ICustomPropertyManager Interface

Solidworks学习笔记-链接Solidworks

允许访问自定义属性。

属性

NameDescription备注
CountGets the number of custom properties.  获取自定义属性的数量。
LinkAllGets or sets whether to link or unlink all custom properties to or from their parent parts.  获取或设置是链接还是取消链接所有自定义属性与其父部件的链接。
OwnerGets the owner of this custom property.  获取此自定义属性的所有者。

方法

NameDescription备注
Add3Adds a custom property to a configuration or model document.  将自定义属性添加到配置或模型文档。
Delete2Deletes the specified custom property.  删除指定的自定义属性。
Get6Gets the value and the evaluated value of the specified custom property.  获取指定自定义属性的值和计算值。
GetAll3Gets all of the custom properties for this configuration.  获取此配置的所有自定义属性。
GetNamesGets the names of the custom properties.  获取自定义属性的名称。
GetType2Gets the type of the specified custom property for a configuration or model document.  获取配置或模型文档的指定自定义属性的类型。
IGetAllGets all of the custom properties for this configuration.  获取此配置的所有自定义属性。
IGetNamesGets the names of the custom properties.  获取自定义属性的名称。
IsCustomPropertyEditableGets whether the specified custom property is editable in the specified configuration.  获取指定的自定义属性在指定的配置中是否可编辑。
LinkPropertySets whether to link or unlink the specified custom property to or from its parent part.  设置是将指定的自定义属性与其父部件链接还是取消链接。
Set2Sets the value for the specified custom property.  设置指定自定义属性的值。
//This example shows how to get the names, types, values, and evaluated values of the //active configuration's custom properties. It also shows how to add a custom property to //the configuration.

//---------------------------------------------------------------------------
// Preconditions:
// 1. Open a part document.
// 2. Open the Immediate window.
//
// Postconditions:
// 1. Adds a date custom property to the part's configuration.
// 2. Tests whether the custom property is editable, and if so,
//    edits it.
// 3. Gets all custom properties in the configuration.
// 4. Deletes a custom property.
// 5. Examine the Immediate window.
//---------------------------------------------------------------------------
 

using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System.Diagnostics;
 
 
 
namespace IsCustomPropEditable_CSharp
{
    public partial class SolidWorksMacro
    {
        public void Main()
        {
            ModelDoc2 swModel;
            Configuration config;
            CustomPropertyManager cusPropMgr;
            int lRetVal;
            object vPropNames = null;
            string[] propNames;
            object vPropTypes = null;
            object vPropValues = null;
            object[] propValues;
            string ValOut;
            string ResolvedValOut;
            bool wasResolved;
            bool linkToProp;
            object resolved = null;
            object linkProp = null;
            int nNbrProps;
            int j;
            int custPropType;
 
            bool bRet;
 
            swModel = (ModelDoc2)swApp.ActiveDoc;
            config = (Configuration)swModel.GetActiveConfiguration();
 
            cusPropMgr = config.CustomPropertyManager;
 
            lRetVal = cusPropMgr.Add3("ADATE", (int)swCustomInfoType_e.swCustomInfoDate, "4-13-59", (int)swCustomPropertyAddOption_e.swCustomPropertyDeleteAndAdd);
            lRetVal = cusPropMgr.Get6("ADATE", false, out ValOut, out ResolvedValOut, out wasResolved, out linkToProp);
            bRet = cusPropMgr.IsCustomPropertyEditable("ADATE", config.Name);
            if (bRet == false)
            {
                Debug.Print("ADATE is editable. Change the date.");
                lRetVal = cusPropMgr.Set2("ADATE", "4-13-89");
            }
            else
            {
                Debug.Print("ADATE is not editable.");
            }
 
            lRetVal = cusPropMgr.Get6("ADATE", false, out ValOut, out ResolvedValOut, out wasResolved, out linkToProp);
 
            // Get the number of custom properties for this configuration
            nNbrProps = cusPropMgr.Count;
            Debug.Print("Number of custom properties for this configuration:            " + nNbrProps);
 
            // Get the custom properties
            lRetVal = cusPropMgr.GetAll3(ref vPropNames, ref vPropTypes, ref vPropValues, ref resolved, ref linkProp);
            propValues = (object[])vPropValues;
            propNames = (string[])vPropNames;
            // For each custom property, print its name, type, and evaluated value
            for (j = 0; j <= nNbrProps - 1; j++)
            {
                custPropType = cusPropMgr.GetType2(propNames[j]);
                Debug.Print("Name, swCustomInfoType_e value, and resolved value:  " + propNames[j] + ", " + custPropType + ", " + propValues[j]);
            }
 
            lRetVal = cusPropMgr.Delete2("ADATE");
 
            // Get the number of custom properties for this configuration
            nNbrProps = cusPropMgr.Count;
            Debug.Print("Number of custom properties for this configuration:            " + nNbrProps);
        }
 
        // The SldWorks swApp variable is pre-assigned for you.
        public SldWorks swApp;
 
    }
}

//This example shows how to get the solid bodies from cut-list folders and how to get the //custom properties for the solid bodies.

//---------------------------------------------------------------
// Preconditions:
// 1. Open public_documents\samples\tutorial\api\weldment_box3.sldprt.
// 2. Click Tools > Options > Document Properties > Weldments >
//    Rename cut list folders with Description property value > OK.
// 3. Right-click Cut list(31) in the FeatureManager design tree
//    and click Update.
// 4. Add a reference to Microsoft.VisualBasic (click Project >
//    Add Reference > Microsoft.VisualBasic).
// 5. Open the Immediate window.
//
// Postconditions:
// 1. Traverses the FeatureManager design tree.
// 2. Examine the Immediate window.
//
// NOTE: Because this part is used elsewhere, do not save changes.
//----------------------------------------------------------------
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System.Runtime.InteropServices;
using System;
using Microsoft.VisualBasic;
using System.Diagnostics;
 
namespace Macro1CSharp.csproj
{
    public partial class SolidWorksMacro
    {
        ModelDoc2 swPart;
        Feature swFeat;
 
        public void Main()
        {
            swPart = (ModelDoc2)swApp.ActiveDoc;
            Debug.Print("File: " + swPart.GetPathName());
            string ConfigName = null;
            ConfigName = swPart.ConfigurationManager.ActiveConfiguration.Name;
            Debug.Print("Active configuration name: " + ConfigName);
            swFeat = (Feature)swPart.FirstFeature();
            TraverseFeatures(swFeat, true, "Root Feature");
 
        }
        public void GetFeatureCustomProps(Feature thisFeat)
        {
            CustomPropertyManager CustomPropMgr = default(CustomPropertyManager);
            CustomPropMgr = (CustomPropertyManager)thisFeat.CustomPropertyManager;
            string[] vCustomPropNames = null;
            vCustomPropNames = (string[])CustomPropMgr.GetNames();
            if ((vCustomPropNames != null))
            {
                Debug.Print("               Cut-list custom properties:");
                int i = 0;
                for (i = 0; i <= (vCustomPropNames.Length - 1); i++)
                {
                    string CustomPropName = null;
                    CustomPropName = (string)vCustomPropNames[i];
                    int CustomPropType = 0;
                    CustomPropType = CustomPropMgr.GetType2(CustomPropName);
                    string CustomPropVal = "";
                    string CustomPropResolvedVal = "";
                    CustomPropMgr.Get2(CustomPropName, out CustomPropVal, out CustomPropResolvedVal);
                    Debug.Print("                     Name: " + CustomPropName);
                    Debug.Print("                         Value: " + CustomPropVal);
                    Debug.Print("                         Resolved value: " + CustomPropResolvedVal);
                }
            }
        }
 
        bool static_DoTheWork_InBodyFolder;
        readonly Microsoft.VisualBasic.CompilerServices.StaticLocalInitFlag static_DoTheWork_BodyFolderType_Init = new Microsoft.VisualBasic.CompilerServices.StaticLocalInitFlag();
        string[] static_DoTheWork_BodyFolderType;
        bool static_DoTheWork_BeenHere;
        public void DoTheWork(Feature thisFeat, string ParentName)
        {
            lock (static_DoTheWork_BodyFolderType_Init)
            {
                try
                {
                    if (InitStaticVariableHelper(static_DoTheWork_BodyFolderType_Init))
                    {
                        static_DoTheWork_BodyFolderType = new string[6];
                    }
                }
                finally
                {
                    static_DoTheWork_BodyFolderType_Init.State = 1;
                }
            }
            bool bAllFeatures = false;
            bool bCutListCustomProps = false;
            object vSuppressed = null;
            int BodyCount = 0;
 
            if (!static_DoTheWork_BeenHere)
            {
                static_DoTheWork_BodyFolderType[0] = "dummy";
                static_DoTheWork_BodyFolderType[1] = "swSolidBodyFolder";
                static_DoTheWork_BodyFolderType[2] = "swSurfaceBodyFolder";
                static_DoTheWork_BodyFolderType[3] = "swBodySubFolder";
                static_DoTheWork_BodyFolderType[4] = "swWeldmentSubFolder";
                static_DoTheWork_BodyFolderType[5] = "swWeldmentCutListFolder";
                static_DoTheWork_InBodyFolder = false;
                static_DoTheWork_BeenHere = true;
                bAllFeatures = false;
                bCutListCustomProps = false;
            }
 
            //Comment out next line to print information for just BodyFolders
            bAllFeatures = true;
            //True to print information about all features    
 
            //Comment out next line if you do not want cut list's custom properties
            bCutListCustomProps = true;
 
            string FeatType = null;
            FeatType = thisFeat.GetTypeName();
            if ((FeatType == "SolidBodyFolder") & (ParentName == "Root Feature"))
            {
                static_DoTheWork_InBodyFolder = true;
            }
            if ((FeatType != "SolidBodyFolder") & (ParentName == "Root Feature"))
            {
                static_DoTheWork_InBodyFolder = false;
            }
 
            //Only consider the CutListFolders that are under SolidBodyFolder
            if ((static_DoTheWork_InBodyFolder == false) & (FeatType == "CutListFolder"))
            {
                //Skip the second occurrence of the CutListFolders during the feature traversal
                return;
            }
 
            //Only consider the SubWeldFolder that are under the SolidBodyFolder
            if ((static_DoTheWork_InBodyFolder == false) & (FeatType == "SubWeldFolder"))
            {
                //Skip the second occurrence of the SubWeldFolders during the feature traversal
                return;
            }
 
            bool IsBodyFolder = false;
            if (FeatType == "SolidBodyFolder" | FeatType == "SurfaceBodyFolder" | FeatType == "CutListFolder" | FeatType == "SubWeldFolder" | FeatType == "SubAtomFolder")
            {
                IsBodyFolder = true;
            }
            else
            {
                IsBodyFolder = false;
            }
            if (bAllFeatures & (!IsBodyFolder))
            {
                Debug.Print("Feature name: " + thisFeat.Name);
                Debug.Print("   Feature type: " + FeatType);
                vSuppressed = thisFeat.IsSuppressed2((int)swInConfigurationOpts_e.swThisConfiguration, null);
                if ((vSuppressed == null))
                {
                    Debug.Print("        Suppression failed");
                }
                else
                {
                    Debug.Print("        Suppressed");
                }
            }
            if (IsBodyFolder)
            {
                BodyFolder BodyFolder = default(BodyFolder);
                BodyFolder = (BodyFolder)thisFeat.GetSpecificFeature2();
                BodyCount = BodyFolder.GetBodyCount();
                if ((FeatType == "CutListFolder") & (BodyCount < 1))
                {
                    //When BodyCount = 0, this cut list folder is not displayed in the
                    //FeatureManager design tree, so skip it
                    return;
                }
                else
                {
                    Debug.Print("Feature name: " + thisFeat.Name);
                    Debug.Print("  Feature type: " + FeatType);
                    vSuppressed = thisFeat.IsSuppressed2((int)swInConfigurationOpts_e.swThisConfiguration, null);
                    if ((vSuppressed == null))
                    {
                        Debug.Print("        Suppression failed");
                    }
                    else
                    {
                        Debug.Print("        Suppressed");
                    }
                }
                if (!bAllFeatures)
                {
                    Debug.Print("Feature name: " + thisFeat.Name);
                    Debug.Print("  Feature type: " + FeatType);
                    vSuppressed = thisFeat.IsSuppressed2((int)swInConfigurationOpts_e.swThisConfiguration, null);
                    if ((vSuppressed == null))
                    {
                        Debug.Print("        Suppression failed");
                    }
                    else
                    {
                        Debug.Print("        Suppressed");
                    }
                }
                int BodyFolderTypeE = 0;
                BodyFolderTypeE = BodyFolder.Type;
                Debug.Print("        Body folder: " + static_DoTheWork_BodyFolderType[BodyFolderTypeE]);
                Debug.Print("        Body folder type: " + BodyFolderTypeE);
                Debug.Print("        Body count: " + BodyCount);
                object[] vBodies = null;
                vBodies = (object[])BodyFolder.GetBodies();
                int i = 0;
                if ((vBodies != null))
                {
                    for (i = 0; i <= (vBodies.Length - 1); i++)
                    {
                        Body2 Body = default(Body2);
                        Body = (Body2)vBodies[i];
                        Debug.Print("          Body name: " + Body.Name);
                    }
                }
            }
            else
            {
                if (bAllFeatures)
                {
                    Debug.Print("");
                }
            }
            if ((FeatType == "CutListFolder"))
            {
                //When BodyCount = 0, this cut-list folder is not displayed
                //in the FeatureManager design tree, so skip it
                if (BodyCount > 0)
                {
                    if (bCutListCustomProps)
                    {
                        //Comment out this line if you do not want to
                        //print the cut-list folder's custom properties
                        GetFeatureCustomProps(thisFeat);
                    }
                }
            }
 
        }
        public void TraverseFeatures(Feature thisFeat, bool isTopLevel, string ParentName)
        {
            Feature curFeat = default(Feature);
            curFeat = thisFeat;
            while ((curFeat != null))
            {
                DoTheWork(curFeat, ParentName);
                Feature subfeat = default(Feature);
                subfeat = (Feature)curFeat.GetFirstSubFeature();
                while ((subfeat != null))
                {
                    TraverseFeatures(subfeat, false, curFeat.Name);
                    Feature nextSubFeat = default(Feature);
                    nextSubFeat = (Feature)subfeat.GetNextSubFeature();
                    subfeat = (Feature)nextSubFeat;
                    nextSubFeat = null;
                }
                subfeat = null;
                Feature nextFeat = default(Feature);
                if (isTopLevel)
                {
                    nextFeat = (Feature)curFeat.GetNextFeature();
                }
                else
                {
                    nextFeat = null;
                }
                curFeat = (Feature)nextFeat;
                nextFeat = null;
            }
        }
 
        static bool InitStaticVariableHelper(Microsoft.VisualBasic.CompilerServices.StaticLocalInitFlag flag)
        {
            if (flag.State == 0)
            {
                flag.State = 2;
                return true;
            }
            else if (flag.State == 2)
            {
                throw new Microsoft.VisualBasic.CompilerServices.IncompleteInitialization();
            }
            else
            {
                return false;
            }
        }
 
 
        /// <summary>
        ///  The SldWorks swApp variable is pre-assigned for you.
        /// </summary>
        public SldWorks swApp;
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值