I'm having the same problem as a recent post here trying to import a Clash Test XML using APIs. Since they don't exist, I'm trying to create my clash test based on a couple of searchs that I add. Here's my code:
// Create 1st Selection Set Search p1st = new Search(); p1st.SearchConditions.Add( new SearchCondition( null, new NamedConstant("LcOaNodeLayer", "Layer"), SearchConditionOptions.IgnoreCategoryName | SearchConditionOptions.IgnorePropertyName, SearchConditionComparison.DisplayStringContains, VariantData.FromDisplayString("Layer1") ) ); SavedItem p1stItem = new SelectionSet(p1st); p1stItem.DisplayName = "Layer1 Contents"; Autodesk.Navisworks.Api.Application.ActiveDocument.SelectionSets.InsertCopy(0, p1stItem); // Create 2nd Selection Set Search p2nd = new Search(); p2nd.SearchConditions.Add( new SearchCondition( null, new NamedConstant("LcOaNodeLayer", "Layer"), SearchConditionOptions.IgnoreCategoryName | SearchConditionOptions.IgnorePropertyName, SearchConditionComparison.DisplayStringContains, VariantData.FromDisplayString("Layer2") ) ); SavedItem p2ndItem = new SelectionSet(p2nd); p2ndItem.DisplayName = "Layer2 Contents"; Autodesk.Navisworks.Api.Application.ActiveDocument.SelectionSets.InsertCopy(0, p2ndItem); // Create the Clash Test ClashTest pTest = new ClashTest(); pTest.CustomTestName = "Layer1 against Layer2"; pTest.DisplayName = pTest.CustomTestName; pTest.TestType = ClashTestType.Hard; pTest.Tolerance = 0; pTest.SelectionA.SelfIntersect = false; pTest.SelectionA.PrimitiveTypes = Autodesk.Navisworks.Api.PrimitiveTypes.Triangles; pTest.SelectionB.SelfIntersect = false; pTest.SelectionB.PrimitiveTypes = Autodesk.Navisworks.Api.PrimitiveTypes.Triangles; ModelItemCollection pSelA = new Autodesk.Navisworks.Api.ModelItemCollection(); ModelItemCollection pSelB = new Autodesk.Navisworks.Api.ModelItemCollection(); //****** How do I get my two Selections into this Clash Test? pClashTests.TestsAddCopy(pTest);
The part I can't figure out is how to use the Selections that I created with the two Layers in the Clash Test that I am trying to add. Any help would be appreciated and please give a complete code example for the missing section in my snipet.
Thanks,
Joe
I've added the following:
ModelItemCollection pSelA = p1st.FindAll(Autodesk.Navisworks.Api.Application.ActiveDocument, false); Selection pSel = new Selection(pSelA); pTest.SelectionA.Selection.CopyFrom(pSel); ModelItemCollection pSelB = p2nd.FindAll(Autodesk.Navisworks.Api.Application.ActiveDocument, false); pSel = new Selection(pSelB); pTest.SelectionB.Selection.CopyFrom(pSel);
This will Create the Clash Test but it's not creating it as a selection source ... it's creating it with explicit items. I need to create it as a selection source. I've poured through labs and docuementation but there is absolutely nothing on creating a Clash Test with two Selection Sources. Please help!
Joe
following up with the update on what I have replied with Joe:
If you set Try-Catch, you would find the exception indicates pTest is read-only. In Navisworks .NET API, the way to edit a object is through the corresponding edit methods, instead of editing the property directly. For some objects, it also needs to create a copy firstly, edit the property of the copy, and copy to the original object. So for your case, the working code should be:
Document oDoc = Autodesk.Navisworks.Api.Application.ActiveDocument; foreach (SavedItem oSS in oDoc.SelectionSets.Value) { { SelectionSet oSet = (SelectionSet)oSS; if (oSet.HasExplicitModelItems) { } if (oSet.HasSearch) { ModelItemCollection oMC = oSet.Search.FindAll(false); Autodesk.Navisworks.Api.Clash.DocumentClash documentClash = oDoc.Clash as Autodesk.Navisworks.Api.Clash.DocumentClash; Autodesk.Navisworks.Api.Clash.DocumentClashTests oDCT = documentClash.TestsData; Autodesk.Navisworks.Api.Clash.ClashTest t = oDCT.Tests[0] as Autodesk.Navisworks.Api.Clash.ClashTest; try { Autodesk.Navisworks.Api.Clash.ClashTest tcopy = t.CreateCopy() as Autodesk.Navisworks.Api.Clash.ClashTest; tcopy.SelectionA.Selection.CopyFrom(oMC); documentClash.TestsData.TestsEditTestFromCopy(t, tcopy); } catch(Exception e){ } }
Hi @xiaodong_liang ,
Thank you for the answer. I am after similar process, in fact I opened a new topic regarding with that (I did not find this conversation beforehand).
Please Click here for more info
I understand I did similar approach as yourself. However I wonder if I understood correctly your post. Do you mean it is not possible to achieve the task via API? It is not possible to assign source explicit? and no matter what I do it will be appear in the nwf by elements in (standards selection in Navis)?
I personally do not mind as I can create a little code to do the process again and again to get all the object before start the process of clash detection, however if another user need to use the nwf the task will be complicate for that particular person.
Is there not workaround on that??
Thank you very much.
Hi @Anonymous /@xiaodong.liang
I am also looking for exactly the same thing. Creating the clash tests based on search sets not by directly selecting the elements. Is there a solution for this or it is a limitation of the API?
Regards
Nayer
var sourceCollectionA = new SelectionSourceCollection();
var sourceCollectionB = new SelectionSourceCollection();
foreach (var savedItem in doc.SelectionSets.Value)
{
foreach (var selectionSet in GetSelectionSets(savedItem, group1.SearchSets.ToList()))
{
sourceCollectionA.Add(doc.SelectionSets.CreateSelectionSource(selectionSet));
}
foreach (var selectionSet in GetSelectionSets(savedItem, group2.SearchSets.ToList()))
{
sourceCollectionB.Add(doc.SelectionSets.CreateSelectionSource(selectionSet));
}
}
clashTest.SelectionA.Selection.CopyFrom(sourceCollectionA);
clashTest.SelectionB.Selection.CopyFrom(sourceCollectionB);
In this case you don't need this method. The main thing you need to understand is that you need SelectionSets. And where you will get them from is the task of your implementation according to your needs
Can't find what you're looking for? Ask the community or share your knowledge.