C# 插入外部块参照后,修改块中的参数值

//更换外部块变量       

private void ChangeParaValue(AcadBlockReference acadref, string[] strPara,string[] strValue)
        {
            dynamic blocks = acadref.Explode();
            acadref.Delete();
            AcadObject[] acdblocks;
            if (((object[])(blocks)).Length > 0)
            {
                acdblocks = new AcadObject[((object[])(blocks)).Length];
                for (int i = 0; i < ((object[])(blocks)).Length; i++)
                {
                    acdblocks[i] = (AcadObject)((object[])(blocks))[i];

                    //MessageBox.Show(acdblocks[i].ObjectName);
                    //更换直线标注
                    if ("AcDbRotatedDimension" == acdblocks[i].ObjectName)
                    {
                        AcadDimRotated rotateDimension = (AcadDimRotated)(acdblocks[i]);
                        MessageBox.Show("Test");
                        for (int j = 0; j < strPara.Length; j++)
                        {                 
                            if (rotateDimension.TextOverride.Contains(strPara[j]))
                            {
                                rotateDimension.TextOverride = rotateDimension.TextOverride.Replace(strPara[j], strValue[j]);
                                break;
                            }                                                
                        }
                    }
                    //更换文本
                    if ("AcDbText" == acdblocks[i].ObjectName)
                    {
                        AcadText acadText = (AcadText)(acdblocks[i]);
                        for (int j = 0; j < strPara.Length; j++)
                        {
                            if (acadText.TextString.Contains(strPara[j]))
                            {
                                acadText.TextString = acadText.TextString.Replace(strPara[j], strValue[j]);
                                break;
                            }
                        }
                    }
                    //更换半径
                    if ("AcDbRadialDimension" == acdblocks[i].ObjectName)
                    {
                        AcadDimRadial radialDimension = (AcadDimRadial)(acdblocks[i]);
                        for (int j = 0; j < strPara.Length; j++)
                        {
                            if (radialDimension.TextOverride.Contains(strPara[j]))
                            {
                                radialDimension.TextOverride = radialDimension.TextOverride.Replace(strPara[j], strValue[j]);
                                break;
                            }
                        }
                    }
                    //更换直径
                    if ("AcDbDiametricDimension" == acdblocks[i].ObjectName)
                    {
                        AcadDimDiametric diametricDimension = (AcadDimDiametric)(acdblocks[i]);
                        for (int j = 0; j < strPara.Length; j++)
                        {
                            if (diametricDimension.TextOverride.Contains(strPara[j]))
                            {
                                diametricDimension.TextOverride = diametricDimension.TextOverride.Replace(strPara[j], strValue[j]);
                                break;
                            }
                        }
                    }
                }

            }

        }

 

 

 

 

 

//实测代码

            AcadBlockReference acadref = m_acadApp.ActiveDocument.ModelSpace.InsertBlock(insertionPoint, "Testblock", 1, 1, 1, 0);//插入到当前文件
            string[] strPara,strValue;
            strPara = new string[] {"H1" ,"T1","R5","D1"};
            strValue = new string[] { "45", "100","23","46" };
            ChangeParaValue(acadref, strPara, strValue);

posted on 2022-04-09 11:29  会叫的鹦鹉  阅读(290)  评论(0编辑  收藏  举报