Continue with last post, we will look further on the API of Clash Detective.
Update Clash
To add or modify clash, we cannot manipulate the properties of the existing objects directly. We need to use the document part: DocumentClashTests. It provides various methods to update the Clash. If there is no corresponding method to update one property such as, say, selection of a test, we need to create a copy of the existing object, modify the copied instance and update the existing object with the copied one. The code below changes the display name and SelectionA of one ClashTest, also changes the status of one ClashResult.
private void modifyClash()
{
Document document = Application.ActiveDocument;
DocumentClash documentClash = document.GetClash();
DocumentClashTests oDCT = documentClash.TestsData;
ClashTest t = oDCT.Tests[0] as ClashTest;
if (t != null)
{
//change the name of one ClashTest
oDCT.TestsEditDisplayName(t, "NewName" +
t.DisplayName);
ClashResult rt = t.Children[0] as ClashResult;
//change the status of one result to Approved
if (rt != null)
oDCT.TestsEditResultStatus(rt,
ClashResultStatus.Approved);
}
//change the SelectionA of the Clash Test.
ClashTest oCopyt = t.CreateCopy() as ClashTest;
//modify SelectionA with the current selection
oCopyt.SelectionA.Selection.CopyFrom(
document.CurrentSelection.SelectedItems);
//modify SelectionA with the current selection
if (document.CurrentSelection.SelectedItems.Count > 0)
{
oCopyt.SelectionA.Selection.CopyFrom(
document.CurrentSelection.SelectedItems);
//update the existing test
oDCT.TestsEditTestFromCopy(t, oCopyt);
}
}
Similarly to modifying clash tests, we can add new clash tests by either creating a new test, or copying from an existing test, and adding it using the methods of DocumentClashTests.
//Sample: Add Clash Test
private void addClashTest()
{
Document document = Application.ActiveDocument;
DocumentClash documentClash = document.GetClash();
DocumentClashTests oDCT = documentClash.TestsData;
//create a ClashTest
ClashTest oNewTest = new ClashTest();
oNewTest.DisplayName = "myTest";
//Set SelectionA and SelectionB of the ClashTest
ModelItemCollection oSelA = new ModelItemCollection();
ModelItemCollection oSelB = new ModelItemCollection();
//assume the first and second model item are not null.
ModelItemEnumerableCollection oModelCollect =
document.Models[0].RootItem.Children;
if(oModelCollect.Count<ModelItem>() > 1)
{
oSelA.Add( oModelCollect .ElementAt<ModelItem>(0));
oSelB.Add( oModelCollect .ElementAt<ModelItem>(1));
oNewTest.SelectionA.Selection.CopyFrom(oSelA);
oNewTest.SelectionB.Selection.CopyFrom(oSelB);
//set other properties of the test …-}
oDCT.TestsAddCopy(oNewTest);
}
The following sample illustrates how we can reorganize the Clash Results by grouping them with ClashResultGroup
// Sample: Add Clash Result Group. Moves all 'NEW' top level results into a group named "Grouped:NEW",
private void addResultsGroup()
{
DocumentClash documentClash = Application.ActiveDocument.GetClash();
DocumentClashTests oDCT = documentClash.TestsData;
ClashTest t = oDCT.Tests[0] as ClashTest;
//add new group if required
ClashResultGroup group;
int groupNdx = t.Children.IndexOfDisplayName("Grouped:NEW");
if (-1 == groupNdx)
{
ClashResultGroup newGroup = new ClashResultGroup();
newGroup.DisplayName = "Grouped:NEW";
oDCT.TestsInsertCopy(t, 0, newGroup);
group = (ClashResultGroup)t.Children[0];
}
else
{
group = (ClashResultGroup)t.Children[groupNdx];
}
//moves all 'NEW' top level issues into the group
int resultsCount = t.Children.Count;
for (int i = resultsCount - 1; i >= 0; i--)
{
SavedItem issue = (SavedItem)t.Children[i];
ClashResult rt = issue as ClashResult;
if (null != rt)
{
if (ClashResultStatus.New == rt.Status)
oDCT.TestsMove(t, i, group, 0);
}
}
}
The content in this article should help you get started with the Clash API. Please refer to API help document for more details. All the codes have been tested with the SDK model clashtest.nwd.
In the next post, we will introduce viewpoints and saved viewpoints.