Drawing sheet PMI settings editor

Hello,

Need some help. I am a beginner and I am writing a journal to edit multiple PMI on drawing.

The code is written to modify Gap size of all existing Horizontal PMI. The code works when all the dimensions on the drawing are PMIHorizontal type. If I add another type of dimension like radial or non-PMIHorizontal or vertical dimensions I get error, but the journal would still modify all the existing Horizontal PMI with the new GAP value.

Please tell me what I am doing wrong.

Thanks
Bhavan

C#Code

using System;
//using System.Collections.Generic;
//using System.ComponentModel;
//using System.Data;
//using System.Drawing;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;
using System.Windows.Forms;
using NXOpen;
using NXOpenUI;
using NXOpen.Annotations;
using NXOpen.UF;

namespace WindowsFormsApplication1
{
public partial class EDIT_DIM : Form
{
public EDIT_DIM()
{
InitializeComponent();
}

public static int GetUnloadOption(string Dummy)
{
return (int)NXOpen.Session.LibraryUnloadOption.Immediately;
}

private void button1_Click(object sender, EventArgs e)
{

}

private void btn_GAP_Click(object sender, EventArgs e)
{
NXOpen.Session theSession = NXOpen.Session.GetSession();
NXOpen.Part workPart = theSession.Parts.Work;
NXOpen.Part displayPart = theSession.Parts.Display;
UFSession.GetUFSession().Abort.DisableAbort();

double GAP = Convert.ToDouble(tbGAP.Text), ViewScale;
NXOpen.NXObject nXObject1;
NXOpen.Annotations.PmiHorizontalDimension pmiHorizontalDimension1;
NXOpen.DisplayableObject[] objects1;
NXOpen.Annotations.EditSettingsBuilder editSettingsBuilder1;
//var /*DIMType,*/ HorDIMType = "NXOpen.Annotations.PmiHorizontalDimension";

foreach (PmiHorizontalDimension PMIHor in workPart.Dimensions)
{
//var DIMType = PMIHor.GetType().ToString();

//if (DIMType != HorDIMType)
//{
// break;
//}
//else if (DIMType == HorDIMType)

{

objects1 = new NXOpen.DisplayableObject[1];
pmiHorizontalDimension1 = PMIHor;

objects1[0] = pmiHorizontalDimension1;
ViewScale = PMIHor.GetAssociativity(1).ObjectView.Scale;

editSettingsBuilder1 = workPart.SettingsManager.CreateAnnotationEditSettingsBuilder(objects1);
editSettingsBuilder1.AnnotationStyle.LineArrowStyle.FirstPosToExtensionLineDistance = GAP * ViewScale;
editSettingsBuilder1.AnnotationStyle.LineArrowStyle.SecondPosToExtensionLineDistance = GAP * ViewScale;

nXObject1 = editSettingsBuilder1.Commit();
editSettingsBuilder1.Destroy();
}
}

}

}
}

Error Image
Screenshot-GAP-Error

Full Error


System.InvalidCastException: Unable to cast object of type 'NXOpen.Annotations.RadiusDimension' to type 'NXOpen.Annotations.PmiHorizontalDimension'.
at WindowsFormsApplication1.EDIT_DIM.btn_GAP_Click(Object sender, EventArgs e) in C:\Users\bhava\Documents\Visual Studio 2015\Projects\PMI_EDITOR\PMI_EDITOR\Form1.cs:line 49
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

Do you want to change only the horizontal dimensions?
If so, you will need to test the type of each dimension and filter out those that are not horizontal dimensions. It looks like there is some code commented out that attempts to do that. I think it is on the right track, but I'd suggest using "continue" instead of "break". Break exits the loop immediately, which you do not want. Continue will skip the rest of the loop and go to the next object in the collection (skipping dimensions that are not the horizontal type).

Do you want to change this setting on all dimensions?
If so, don't cast the dimensions to the PmiHorizontalDimension type, keep them as the Dimension type.

Yes, as an initial test code I only wanted to do it for Horizontal type.

Continue instead of Break didn't work.

However, I was able to suppress the error using Try{ } Catch{ }.

Thanks.

BH