C# SolidWorks 二次开发 API-替换工程图视图引用

126 篇文章 73 订阅
55 篇文章 15 订阅

1024-程序员的节日。
但为什么不放假呢?
今天主要是为了拿个徽章,顺便完成这个月的小目标。
在这里插入图片描述
这个功能之前提过,感觉好像有很多尺寸的时候关联可能会丢失吧。
但想想这个有些情况还可能用的到,就先写下来充个数吧。

        /// <summary>
        /// 工程图替换文件引用
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnReplaceModelForView_Click(object sender, EventArgs e)
        {
            var actPath = RegDllPath("");

            var start = actPath.Substring(0, actPath.IndexOf("CSharpAndSolidWorks", 0));

            var oldPartName = $@"{start}CSharpAndSolidWorks\CSharpAndSolidWorks\TemplateModel\replaceDrawingRef\AA(BB).SLDPRT";

            var newPartName = $@"{start}CSharpAndSolidWorks\CSharpAndSolidWorks\TemplateModel\replaceDrawingRef\AA(BB) - 副本.SLDPRT";

            var swApp = PStandAlone.GetSolidWorks();

            var swModel = (ModelDoc2)swApp.ActiveDoc;

            var docModel = swModel as DrawingDoc;

            var views = (object[])docModel.GetViews();

            List<View> views1 = new List<View>();
            List<Component2> comps1 = new List<Component2>();


            for (int i = 0; i < views.Length; i++)
            {
                var tempV = (object[])views[i];

                if (tempV.Length>1)
                {
                    for (int j =1; j < tempV.Length; j++)
                    {
                        var tempView = (View)tempV[j];
                        if (tempView.RootDrawingComponent.Component!=null)
                        {
                            if (tempView.RootDrawingComponent.Component.IGetModelDoc().GetPathName().ToUpper() == oldPartName.ToUpper())
                            {
                                views1.Add(tempView);
                                comps1.Add(tempView.RootDrawingComponent.Component);
                            }

                        }
                        else
                        {
                            var visComps = (object[])tempView.GetVisibleComponents();

                            if (tempView.GetVisibleComponentCount()==1 && ((visComps[0] as Component2).IGetModelDoc().GetPathName().ToUpper() == oldPartName.ToUpper()))
                            {
                                views1.Add(tempView);
                                comps1.Add(visComps[0] as Component2);
                            }

                        }

                    }

                }


            }

    

            //DispatchWrapper[] viewsIn = new DispatchWrapper[views1.Count];
            //DispatchWrapper[] instancesIn = new DispatchWrapper[views1.Count];




            var res= docModel.ReplaceViewModel(newPartName, views1.ToArray(), comps1.ToArray());

        }

代码就这些了,也就不细讲了。
主要就是遍历所有视图,然后传对应的参考组件,返回替换结果 。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Paine Zeng

如果对有帮助,请我喝咖啡吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值