参照案例写了一段获取模板的代码:
string strXmlXPath = @"Templates\Rail\Double Track -Ballasted"; string strLib = TemplateLibrary.GetDefaultTemplateLibraryPath(); TemplateLibrary templateLibrary = TemplateLibrary.Load(strLib); TemplateDefinition tempDef2 = templateLibrary.FindTemplateByPath(strXmlXPath);
但是,返现获取到的TemplateDefinition为Null。
请问有可能是什么原因呢?路径我检查了下应该是没问题的。
下面的代码示例通过TemplateLibrary对象遍历模板库中所有的模板对象;以及获取模板库中指定路径的模板对象:
class TemplateCmd { public void GetTemplate() { string strLib = TemplateLibrary.GetDefaultTemplateLibraryPath(); TemplateLibrary templateLibrary = TemplateLibrary.Load(strLib); //从模板库的根目录开始遍历。 GetCategoryAllTemplate(templateLibrary.RootCategory); //以Xml Xpath形式获取指定的模板 string strXmlXPath = @"/InRoads/TemplateLibrary/Category/Category[@name='Templates']/Category[@name='Rail']/Template[@name='Double Track - Wooden Sleepers']"; //TemplateDefinition tempDef = templateLibrary.FindTemplateByPath(strXmlXPath); //以xml xpath路径对应的模板库路径形式的路径 string strTemplatePath = @"Templates/Rail/Double Track - Wooden Sleepers"; TemplateDefinition tempDef2 = templateLibrary.FindTemplateByPath(strTemplatePath); } private void GetCategoryAllTemplate(Category category) { //遍历目录下的子目录 foreach(Category subCategory in category.Categories) { GetCategoryAllTemplate(subCategory); } //表面目录下的所有模板 foreach(TemplateDefinition tempDef in category.Templates) { string strName = tempDef.Name; string strPath = tempDef.GetPath(); } } }
张总,我测试了下您给的这个@"Templates/Rail/Double Track - Wooden Sleepers";模板路径,在调用FindTemplateByPath函数的时候抛出了异常:
然后我还测试了@"/InRoads/TemplateLibrary/Category/Category[@name='Templates']/Category[@name='Rail']/Template[@name='Double Track - Wooden Sleepers']"这个路径,报了同样的错误。
那就得怀疑您的环境了。请告知所有软件的详细名称和版本以及VS的详细版本。
另外就是在您的ORD软件启动后打开一个DGN,是否从模板界面中能看到我上图所示的这个模板名称?(主要是验证在您的环境下模板是否被正确加载了)
是这样的,付老师。我的电脑上安装了两个版本的ORD:2019 CE版和2021 China版。(VS版本是2017 Community)
2019 CE版的模板相关功能都正常,但是2021 China版我在之前打开模板界面的时候,提示说找不到“CIVIL_ROADWAY_TEMPLATE_LIBRARY"的变量配置,然后我把这个变量配置到了China版的环境变量配置里面。
现在China版是能够看到模板的:
但是,如上图所示,这个模板看起来不太正常,尤其是变坡那些End condition显示的是白色(一般应该为绿色的线条)。
然后我双击模板中的一个组件,会弹出如下提示:
我不确定这个问题和我调用SDK里面的FindTemplateByPath函数获取不到模板有没有关系,我用的代码:
string strLib = TemplateLibrary.GetDefaultTemplateLibraryPath(); TemplateLibrary templateLibrary = TemplateLibrary.Load(strLib); TemplateDefinition tempDef2 = templateLibrary.FindTemplateByPath(strXmlXPath);
GetDefaultTemplateLibraryPath这一步我确定是能获取到这个模板文件的路径的。
是不是有可能是因为安装了两个版本ORD的关系?
付老师,我今天重新把ORD中国版安装了一下,安装了一个ORD China Ultimate版本。
但是还是无法通过FindTemplateByPath方法获取到TemplateDefinition。
我检查了多次,所使用模板文件和路径应该都是没问题的,但是依然弹出了这个异常:
这是我用的模板路径:
string strXmlXPath = @"/InRoads/TemplateLibrary/Category/Category[@name='Templates']/Category[@name='Rail']/Template[@name='Double Track - Ballasted']";
这是我尝试去读取的模板文件:
我这边用中国版是可以的:
您先在下面代码strName 处设个断点或者进行判断,看看有没有您需要的模板:
foreach ( TemplateDefinition tempDef in category.Templates ) { string strName = tempDef.Name; string strPath = tempDef.GetPath(); }
Answer Verified By: fanghua wei
今天尝试了下把这些模板的路径都输出到一个文件里面,然后进行检查。
发现路径还是要用“\\“来进行分割:
string temPath2 = "Templates\\Rail\\Double Track - Ballasted";
TemplateDefinition templateDefinition = GetTemplate(temPath2); TemplateDrop templateDrop = ApplyTemple(corridor, corridor.StartDistance, corridor.EndDistance, templateDefinition); if (templateDrop == null) { return; }
这样子能够获取到模板的信息。谢谢各位。