using System;
using NXOpen;
using NXOpen.UF;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
public class Program
{
[DllImport("pskernel.dll", EntryPoint = "PK_BODY_create_solid_block")]
unsafe public static extern void create_solid_block(double x, double y, double z, AXIS2_sf_t* basis_set, BODY_t* body);
public struct VECTOR_t
{
public unsafe VECTOR_t(double[] coord)
{
fixed (double* ptr = &this.coord.FixedElementField)
{
*ptr = coord[0];
}
fixed (double* ptr = &this.coord.FixedElementField)
{
ptr[1] = coord[1];
}
fixed (double* ptr = &this.coord.FixedElementField)
{
ptr[2] = coord[2];
}
}
public unsafe VECTOR_t(double coordI0, double coordI1, double coordI2)
{
fixed (double* ptr = &this.coord.FixedElementField)
{
*ptr = coordI0;
}
fixed (double* ptr = &this.coord.FixedElementField)
{
ptr[1] = coordI1;
}
fixed (double* ptr = &this.coord.FixedElementField)
{
ptr[2] = coordI2;
}
}
//[FixedBuffer(typeof(double), 3)]
public VECTOR_t.e__FixedBufferd coord;
[UnsafeValueType]
[CompilerGenerated]
[StructLayout(LayoutKind.Sequential, Size = 24)]
public struct e__FixedBufferd
{
public double FixedElementField;
}
}
public struct VECTOR1_t
{
public unsafe VECTOR1_t(double[] coord)
{
fixed (double* ptr = &this.coord.FixedElementField)
{
*ptr = coord[0];
}
fixed (double* ptr = &this.coord.FixedElementField)
{
ptr[1] = coord[1];
}
fixed (double* ptr = &this.coord.FixedElementField)
{
ptr[2] = coord[2];
}
}
public unsafe VECTOR1_t(double coordI0, double coordI1, double coordI2)
{
fixed (double* ptr = &this.coord.FixedElementField)
{
*ptr = coordI0;
}
fixed (double* ptr = &this.coord.FixedElementField)
{
ptr[1] = coordI1;
}
fixed (double* ptr = &this.coord.FixedElementField)
{
ptr[2] = coordI2;
}
}
//[FixedBuffer(typeof(double), 3)]
public VECTOR1_t.e__FixedBufferc coord;
[CompilerGenerated]
[UnsafeValueType]
[StructLayout(LayoutKind.Sequential, Size = 24)]
public struct e__FixedBufferc
{
public double FixedElementField;
}
}
public struct AXIS2_sf_t
{
public AXIS2_sf_t(VECTOR_t location, VECTOR1_t axis, VECTOR1_t ref_direction)
{
this.location = location;
this.axis = axis;
this.ref_direction = ref_direction;
}
public VECTOR_t location;
public VECTOR1_t axis;
public VECTOR1_t ref_direction;
}
public struct BODY_t
{
public BODY_t(int value)
{
this.value = value;
}
public int Value
{
get
{
return this.value;
}
set
{
this.value = value;
}
}
public static implicit operator int(BODY_t operand)
{
return operand.value;
}
public static implicit operator BODY_t(int operand)
{
return new BODY_t(operand);
}
//下面的类型转换会涉及到新的结构体类型,照理说应该将其声明出来,但是又会延伸出新的结构体类型,这样没完没了的数量又大,用不上的索性注释掉
//public static implicit operator PART_t(BODY_t operand)
//{
// return new PART_t(operand.value);
//}
//public static implicit operator TOPOL_t(BODY_t operand)
//{
// return new TOPOL_t(operand.value);
//}
//public static implicit operator ENTITY_t(BODY_t operand)
//{
// return new ENTITY_t(operand.value);
//}
private int value;
public static readonly BODY_t @null = default(BODY_t);
}
// class members
private static Session theSession;
private static UFSession theUfSession;
public static Program theProgram;
public static bool isDisposeCalled;
//------------------------------------------------------------------------------
// Constructor
//------------------------------------------------------------------------------
public Program()
{
try
{
theSession = Session.GetSession();
theUfSession = UFSession.GetUFSession();
isDisposeCalled = false;
}
catch (NXOpen.NXException ex)
{
// ---- Enter your exception handling code here -----
// UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, ex.Message);
}
}
//------------------------------------------------------------------------------
// Explicit Activation
// This entry point is used to activate the application explicitly
//------------------------------------------------------------------------------
unsafe public static int Main(string[] args)
{
int retValue = 0;
try
{
theProgram = new Program();
Tag partition, ug_tag;
theUfSession.Ps.CreatePartition(out partition);
VECTOR_t location = new VECTOR_t(0.01, 0.01, 0.01);
VECTOR1_t axis = new VECTOR1_t(0, 0, 1);
VECTOR1_t ref_direction = new VECTOR1_t(1, 0, 0);
AXIS2_sf_t axis2 = new AXIS2_sf_t(location, axis, ref_direction);
BODY_t body;
create_solid_block(0.1, 0.2, 0.3, &axis2, &body);
theUfSession.Ps.CreateObjFromPsTag((Tag)body.Value, out ug_tag);
theUfSession.Disp.RegenerateDisplay();
theUfSession.Modl.Update(); //部件导航器显示创建对象名称
//TODO: Add your application code here
theProgram.Dispose();
}
catch (NXOpen.NXException ex)
{
// ---- Enter your exception handling code here -----
}
return retValue;
}
//------------------------------------------------------------------------------
// Following method disposes all the class members
//------------------------------------------------------------------------------
public void Dispose()
{
try
{
if (isDisposeCalled == false)
{
//TODO: Add your application code here
}
isDisposeCalled = true;
}
catch (NXOpen.NXException ex)
{
// ---- Enter your exception handling code here -----
}
}
public static int GetUnloadOption(string arg)
{
//Unloads the image explicitly, via an unload dialog
//return System.Convert.ToInt32(Session.LibraryUnloadOption.Explicitly);
//Unloads the image immediately after execution within NX
return System.Convert.ToInt32(Session.LibraryUnloadOption.Immediately);
//Unloads the image when the NX session terminates
// return System.Convert.ToInt32(Session.LibraryUnloadOption.AtTermination);
}
}
C#使用PK函数,都知道PK函数放在pskernel.dll这个文件中,既然放在dll中,那C#就可以尝试用静态或者动态的方式去调用,而调用首先尝试的是签名这个环节,到底该怎么写,可以去翻PK帮助文件,因为是针对C++的所以帮助不大,还好UG10.0起提供一个关键文件pskernel_net.dll,这就不得了了,专门为.net用户量身打造的,C#引用进来一看便知再也不用猜来猜去。签名环节解决了剩下的就是看着就头大记也记不住的自定义的类型怎么办,这里必须要提到一个反编译工具dnSpy,简直神器,对pskernel_net.dll进行反编译后一目了然,也可以说一步到位,因为他里面把函数签名定义的类型以源码的形式呈现出来,直接复制出来用就行了,当然会涉及到一些微改,但问题不大。看到自定义类型,其实就是一个结构体,以我半碗水的水平想到死都想不出来。这个世界真的很复杂。
代码中使用了关键字unsafe,需在项目属性中,生成选项卡勾选“允许不安全代码”。
以一句废话来结尾…