Пример #1
0
    /// <summary>
    /// Get the dynamic property value
    /// </summary>
    public static object GetPropertyValue(CadDynamicBlock dynamicBlock, string propertyName)
    {
        if (dynamicBlock == null)
        {
            return(null);
        }
        if (string.IsNullOrEmpty(propertyName))
        {
            return(null);
        }

        var block = dynamicBlock.Block;

        using (var trans = block.Database.TransactionManager.StartTransaction())
        {
            var btr = (BlockTableRecord)trans.GetObject(block.DynamicBlockTableRecord, OpenMode.ForRead, false, true);

            if (btr.IsDynamicBlock)
            {
                var dynBlock = (BlockReference)trans.GetObject(block.Id, OpenMode.ForRead, false, true);
                foreach (DynamicBlockReferenceProperty dbrProperty in dynBlock.DynamicBlockReferencePropertyCollection)
                {
                    //var values = dbrProperty.GetAllowedValues();
                    if (dbrProperty.PropertyName == propertyName)
                    {
                        return(dbrProperty.Value);
                    }
                }
            }

            return(null);
        }
    }
Пример #2
0
    /// <summary>
    /// Get the attribute value
    /// </summary>
    public static string GetAttributeValue(CadDynamicBlock dynamicBlock, string attributeName)
    {
        if (dynamicBlock == null)
        {
            return(null);
        }
        if (string.IsNullOrEmpty(attributeName))
        {
            return(null);
        }

        var block = dynamicBlock.Block;

        using (var trans = block.Database.TransactionManager.StartTransaction())
        {
            foreach (ObjectId attId in block.AttributeCollection)
            {
                var att = (AttributeReference)trans.GetObject(attId, OpenMode.ForRead, false, true);

                if (att.Tag == attributeName)
                {
                    return(att.IsMTextAttribute ? att.MTextAttribute.Text : att.TextString);
                }
            }

            trans.Commit();
        }

        using (var trans = block.Database.TransactionManager.StartTransaction())
        {
            var btr = (BlockTableRecord)trans.GetObject(block.BlockTableRecord, OpenMode.ForRead, false, true);

            if (btr.HasAttributeDefinitions)
            {
                foreach (var oid in btr)
                {
                    var dbObject = trans.GetObject(oid, OpenMode.ForRead, false, true);

                    if (dbObject is AttributeDefinition att)
                    {
                        if (att.Tag == attributeName)
                        {
                            return(att.IsMTextAttributeDefinition ? att.MTextAttributeDefinition.Text : att.TextString);
                        }
                    }
                }
            }

            trans.Commit();
        }

        return(null);
    }
Пример #3
0
    /// <summary>
    /// Rotate a CadDynamicBlock based on a given normal and angle
    /// </summary>
    public static CadDynamicBlock ByRotation(CadDynamicBlock dynamicBlock, Vector normal, double degree)
    {
        if (dynamicBlock == null)
        {
            return(null);
        }

        var block        = dynamicBlock.Block;
        var normalVector = new Vector3d(normal.X, normal.Y, normal.Z);

        block.TransformBy(Matrix3d.Rotation(degree * Math.PI / 180, normalVector, block.Position));

        return(new CadDynamicBlock(block, false));
    }
Пример #4
0
    /// <summary>
    /// Set the dynamic property value
    /// </summary>
    public static bool SetPropertyValue(CadDynamicBlock dynamicBlock, string propertyName, object propertyValue)
    {
        if (dynamicBlock == null)
        {
            return(false);
        }
        if (string.IsNullOrEmpty(propertyName))
        {
            return(false);
        }
        if (propertyValue == null)
        {
            return(false);
        }

        var block = dynamicBlock.Block;

        using (var trans = block.Database.TransactionManager.StartTransaction())
        {
            var btr = (BlockTableRecord)trans.GetObject(block.DynamicBlockTableRecord, OpenMode.ForRead, false, true);

            if (btr.IsDynamicBlock)
            {
                var dynBlock   = (BlockReference)trans.GetObject(block.Id, OpenMode.ForWrite, false, true);
                var properties = dynBlock.DynamicBlockReferencePropertyCollection;

                foreach (DynamicBlockReferenceProperty property in properties)
                {
                    if (property.PropertyName == propertyName)
                    {
                        switch ((DwgDataType)property.PropertyTypeCode)
                        {
                        case DwgDataType.Real:
                        {
                            if (propertyValue is double)
                            {
                                property.Value = propertyValue;
                                return(true);
                            }
                            break;
                        }

                        case DwgDataType.Int16:
                        case DwgDataType.Int32:
                        {
                            if (Convert.GetTypeCode(propertyValue) == TypeCode.Int16 ||
                                Convert.GetTypeCode(propertyValue) == TypeCode.Int32)
                            {
                                var value = Convert.ToInt32(propertyValue);

                                // TODO: why is this?
                                if (value == 0 || value == 1)
                                {
                                    property.Value = Convert.ToInt16(propertyValue);
                                    return(true);
                                }
                            }
                            break;
                        }

                        case DwgDataType.Text:
                        {
                            if (propertyValue is string)
                            {
                                property.Value = propertyValue;
                                return(true);
                            }

                            break;
                        }

                        case DwgDataType.Dwg3Real:
                        {
                            if (propertyValue is Point dynamoPoint)
                            {
                                property.Value = new Point3d(dynamoPoint.X, dynamoPoint.Y, dynamoPoint.Z);
                                return(true);
                            }
                            break;
                        }

                        default:
                        {
                            return(false);
                        }
                        }
                    }
                }
            }

            trans.Commit();
        }

        return(default);