Ejemplo n.º 1
0
        /// <summary>
        /// 获取选中对象,通过类型和标记
        /// Return selected objects filtered by type and mark.
        /// </summary>
        /// <param name="selMgr"></param>
        /// <param name="filter">(type,mark)=>bool</param>
        /// <returns></returns>
        public static IEnumerable <object> GetSelectedObjects(this IMacroFeatureData selMgr, Func <swSelectType_e, int, bool> filter)
        {
            {
                object objects;
                object objectTypes;
                object marks;
                object drViews;
                object componentXForms;
                selMgr.GetSelections3(out objects, out objectTypes, out marks, out drViews, out componentXForms);

                var objectsArray = (object[])objects;
                var typesArray   = (swSelectType_e[])objectTypes;
                var marksArray   = (int[])marks;

                var i = 0;
                foreach (var item in objectsArray)
                {
                    if (filter(typesArray[i], marksArray[i]))
                    {
                        yield return(item);
                    }
                    i++;
                }
            }
        }
Ejemplo n.º 2
0
        internal object GetParameters(IFeature feat, IMacroFeatureData featData, IModelDoc2 model, Type paramsType,
                                      out IDisplayDimension[] dispDims, out string[] dispDimParams, out IBody2[] editBodies, out MacroFeatureOutdateState_e state)
        {
            object retParamNames  = null;
            object retParamValues = null;
            object paramTypes     = null;
            object retSelObj;
            object selObjType;
            object selMarks;
            object selDrViews;
            object compXforms;

            featData.GetParameters(out retParamNames, out paramTypes, out retParamValues);
            featData.GetSelections3(out retSelObj, out selObjType, out selMarks, out selDrViews, out compXforms);

            IDisplayDimension[] localDispDims = null;
            var dispDimParamsMap = new SortedDictionary <int, string>();

            try
            {
                var dispDimsObj = featData.GetDisplayDimensions() as object[];

                if (dispDimsObj != null)
                {
                    localDispDims = new IDisplayDimension[dispDimsObj.Length];

                    for (int i = 0; i < localDispDims.Length; i++)
                    {
                        localDispDims[i] = dispDimsObj[i] as IDisplayDimension;
                        dispDimsObj[i]   = null;
                    }
                }
                else
                {
                    localDispDims = null;
                }

                object[] editBodiesObj = featData.EditBodies as object[];

                IBody2[] localEditBodies = null;

                if (editBodiesObj != null)
                {
                    localEditBodies = editBodiesObj.Cast <IBody2>().ToArray();
                }

                var paramNames  = retParamNames as string[];
                var paramValues = retParamValues as string[];
                var selObjects  = retSelObj as object[];

                var parameters = new Dictionary <string, string>();

                var paramsVersion = new Version();
                var dimsVersion   = new Version();

                if (paramNames != null && paramValues != null)
                {
                    for (int i = 0; i < paramNames.Length; i++)
                    {
                        if (paramNames[i] == VERSION_PARAMETERS_NAME)
                        {
                            paramsVersion = new Version(paramValues[i]);
                        }
                        else if (paramNames[i] == VERSION_DIMENSIONS_NAME)
                        {
                            paramsVersion = new Version(paramValues[i]);
                        }
                        else
                        {
                            parameters.Add(paramNames[i], paramValues[i]);
                        }
                    }
                }

                ConvertParameters(paramsType, paramsVersion, conv =>
                {
                    parameters      = conv.ConvertParameters(model, feat, parameters);
                    localEditBodies = conv.ConvertEditBodies(model, feat, localEditBodies);
                    selObjects      = conv.ConvertSelections(model, feat, selObjects);
                    localDispDims   = conv.ConvertDisplayDimensions(model, feat, localDispDims);
                });

                var resParams = Activator.CreateInstance(paramsType);

                TraverseParametersDefinition(resParams.GetType(),
                                             (prp) =>
                {
                    AssignObjectsToProperty(resParams, selObjects, prp, parameters);
                },
                                             (dimType, prp) =>
                {
                    var dimIndices = GetObjectIndices(prp, parameters);

                    if (dimIndices.Length != 1)
                    {
                        throw new InvalidOperationException(
                            "It could only be one index associated with dimension");
                    }

                    var dimInd = dimIndices.First();

                    if (localDispDims.Length > dimInd)
                    {
                        var dispDim = localDispDims[dimInd];

                        if (!(dispDim is DisplayDimensionPlaceholder))
                        {
                            var dim = dispDim.GetDimension2(0);
                            var val = (dim.GetSystemValue3(
                                           (int)swInConfigurationOpts_e.swSpecifyConfiguration,
                                           new string[] { featData.CurrentConfiguration.Name }) as double[])[0];
                            prp.SetValue(resParams, val, null);
                        }

                        dispDimParamsMap.Add(dimInd, prp.Name);
                    }
                    else
                    {
                        throw new IndexOutOfRangeException(
                            $"Dimension at index {dimInd} id not present in the macro feature");
                    }
                },
                                             (prp) =>
                {
                    AssignObjectsToProperty(resParams, localEditBodies, prp, parameters);
                },
                                             prp =>
                {
                    var paramVal = GetParameterValue(parameters, prp.Name);
                    var val      = Convert.ChangeType(paramVal, prp.PropertyType);
                    prp.SetValue(resParams, val, null);
                });

                dispDims      = localDispDims;
                editBodies    = localEditBodies;
                state         = GetState(featData, localDispDims);
                dispDimParams = dispDimParamsMap.Values.ToArray();

                return(resParams);
            }
            catch
            {
                ReleaseDisplayDimensions(localDispDims);

                throw;
            }
        }