22/05/2025

Gán giá trị dynamic cho block BlockReference | AutoCAD dotNet

Ứng dụng được phát triển/Sưu tầm bởi đội ngũ AutoLISP Thật là đơn giản
   

Thông tin thêm: 👉👉👉

Thay đổi thuộc tính Dynamic của Block 



1 Thêm class BlockReferenceExtensions.cs

Lưu mã sau dưới dạng tệp tin BlockReferenceExtensions.cs
Code:
internal static class BlockReferenceExtensions
{
    public static void SetPros(this BlockReference br, params object[] objs)
    {
        if (br is null)
            return;
        string pros = null;

        foreach (DynamicBlockReferenceProperty prop in br.DynamicBlockReferencePropertyCollection)
        {
            for (int i = 0, loopTo = objs.Length - 1; i <= loopTo; i += 2)
            {
                pros = objs[i] as string;
                if (pros != null)
                {
                    var value = objs[i + 1];
                    try
                    {
                        if (prop.PropertyName.ToUpper() == pros.ToUpper() && !prop.ReadOnly)
                        {
                            if (prop.UnitsType == DynamicBlockReferencePropertyUnitsType.NoUnits)
                            {
                                if (prop.Value.GetType() == typeof(short))
                                {
                                    prop.Value = (short)value.ToString().CInt(0);
                                }
                                else
                                {
                                    prop.Value = value.ToString();
                                }
                            }
                            else if (prop.UnitsType == DynamicBlockReferencePropertyUnitsType.Angular)
                            {
                                prop.Value = value.CDouble(1.0d) * Math.PI / 180.0d;
                            }
                            else
                            {
                                prop.Value = value.CDouble(1.0d);
                            }

                            break;
                        }
                    }
                    catch (Exception ee)
                    {
                        //EDs.Princ("SetPros Error at: " + prop.PropertyName + Operators.AddObject(" ", value) + " " + ee.Message);
                    }
                }
            }
        }
    }

    public static int CInt(this object str, int def = 0)
    {
        if (str == null) return def;
        int retval = def;
        int.TryParse(str.ToString(), out retval);
        return retval;
    }
    public static double CDouble(this object obj, double def = 0.0d)
    {
        if (str == null) return def;
    double retval = def;     try     {      double.TryParse(obj.ToString(), out retval);     }
        catch
        {
        }
        return retval;
    }
}


2 Sử dụng hàm SetPros

Lưu mã sau 
Code:
br.SetPros("Distance1", 100, "Angle1", 90, "Flip1", 1)


Link tải (MediaFire)

📥 https://www.mediafire.com/



---------------------------------------------------------------------------------------------
Ứng dụng được phát triển bởi đội ngũ AutoLISP Thật là đơn giản - Tác giả ứng dụng in D2P

    

Mọi thông tin xin liên hệ Fanpage AutoLISP Thật là đơn giản!
Cảm ơn bạn đã theo dõi!

Không có nhận xét nào:

Đăng nhận xét

Hàm COUNTIFS đếm theo nhiều điều kiện trong Excel | COUNTIFS, COUNTIF | AutoLISP Reviewer

Hàm COUNTIFS đếm theo nhiều điều kiện trong Excel Cuối tháng cần biết phòng Kế toán có bao nhiêu người đi làm đủ công, bao nhiêu đơn hàng ...