C# SolidWorks 二次开发 API---替换工程图文件关系

这篇博文讲的是在某些情况下需要把工程图中的模型进行替换。其实代码写好很久了,没有时间写博客

比如: 本来有A.sldprt和A.slddrw这两个文件,是A零件的模型与A零件的工程图。
我们需要去利用A零件衍生零件B,就改一些尺寸,特征什么的。
正常情况的操作是利用打包功能把A零件与工程图打包并改名为B.sldprt和B.slddrw.
在这里插入图片描述

但是也不排除经常会先直接把零件B做完了。然后没有图纸,

这样笨办法比较多,重新出图或者重新打包后改零件。
在这里插入图片描述
熟悉的人知道工程图中有下面这个替换模型:
在这里插入图片描述
这一步操作下来,你的图纸模型换过来了,但是有时候一些明明同样的参考关系尺寸会找不到参考了。这个bug Solidworks好像还存在,有时候没问题。

更加熟悉软件的人可能 会注意到,打开零件图纸的时候,有一个修改参考关系的选项。
在这里插入图片描述
这样可以修改参考的

在这里插入图片描述

这一步操作使用起来好像没有报错,我们来看一下这一步的实现,要实现引用关系的修改,其实只需要利用Document Manager来修改,对于装配体 下的零件改名,也是很好的办法。改完之后 重新修改下装配体的引用关系。

 private void btnReplaceReference_Click(object sender, EventArgs e)
        {
    
    
            const string sLicenseKey = "yourLicenseKey";//如果正版用户,请联系代理商申请。

            string sDocFileName = @"E:\01_Work\22_Gitee\CSharpAndSolidWorks\CSharpAndSolidWorks\TemplateModel\repleaceReference\part1.SLDDRW";

            SwDMClassFactory swClassFact = default(SwDMClassFactory);
            SwDMApplication swDocMgr = default(SwDMApplication);
            SwDMDocument swDoc = default(SwDMDocument);
            SwDMDocument10 swDoc10 = default(SwDMDocument10);
            SwDMDocument22 swDoc22 = default(SwDMDocument22);

            SwDmDocumentType nDocType = 0;
            SwDmDocumentOpenError nRetVal = 0;
            SwDmPreviewError nError = 0;

            // Determine type of SOLIDWORKS file based on file extension
            if (sDocFileName.ToLower().EndsWith("sldprt"))
            {
    
    
                nDocType = SwDmDocumentType.swDmDocumentPart;
            }
            else if (sDocFileName.ToLower().EndsWith("sldasm"))
            {
    
    
                nDocType = SwDmDocumentType.swDmDocumentAssembly;
            }
            else if (sDocFileName.ToLower().EndsWith("slddrw"))
            {
    
    
                nDocType = SwDmDocumentType.swDmDocumentDrawing;
            }
            else
            {
    
    
                // Probably not a SOLIDWORKS file,
                // so cannot open
                nDocType = SwDmDocumentType.swDmDocumentUnknown;
                return;
            }

            swClassFact = new SwDMClassFactory();
            swDocMgr = (SwDMApplication)swClassFact.GetApplication(sLicenseKey);
            swDoc = (SwDMDocument)swDocMgr.GetDocument(sDocFileName, nDocType, false, out nRetVal);

            swDoc10 = (SwDMDocument10)swDoc;
            swDoc22 = (SwDMDocument22)swDoc;

            object vBrokenRefs = null;
            object vIsVirtuals = null;
            object vTimeStamps = null;
            object vIsImported = null;

            string[] vDependArr = null;

            SwDMSearchOption swSearchOpt = default(SwDMSearchOption);

            swSearchOpt = swDocMgr.GetSearchOptionObject();

            vDependArr = (string[])swDoc22.GetAllExternalReferences5(swSearchOpt, out vBrokenRefs, out vIsVirtuals, out vTimeStamps, out vIsImported);

            if ((vDependArr == null)) return;

            var doc16 = (SwDMDocument16)swDoc;

            doc16.ReplaceReference(vDependArr[0], @"E:\01_Work\22_Gitee\CSharpAndSolidWorks\CSharpAndSolidWorks\TemplateModel\repleaceReference\part1new.SLDPRT");

            swDoc.Save();

            swDoc.CloseDoc();
        }

在这里插入图片描述

一切都在源码中,请自行下载。记得加个关注,点个赞哈。。。
如果有想法,来发表意见。
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/zengqh0314/article/details/111146771
今日推荐