Creo二次开发基于C++、C#混合编程

Creo二次开发基于C++、C#混合编程

1.创建C++动态链接库项目

在这里插入图片描述

2.编辑C++函数代码dllmain.cpp

#include "pch.h"
#include <ProMdl.h>
#include <ProToolkit.h>
#include <ProUtil.h>
#include <ProMessage.h>
#include <windows.h>

extern "C" __declspec(dllexport) ProMdlType MdlRetrieve(char *name,ProMdlType type,ProMdl **mdl)
{
	
	mdl = NULL;
    //宽字符串转换为多字节字符串
    int bufSize = MultiByteToWideChar(CP_ACP, 0, name, -1, NULL, 0);
    wchar_t* wp = new wchar_t[bufSize];
    MultiByteToWideChar(CP_ACP, 0, name, -1, wp, bufSize);

	ProError err = ProMdlRetrieve(wp, type, *p_handle);
	return type;
}

extern "C" __declspec(dllexport) int Sub(int a,int b)
{
	int c = a+b;
	return c;
}

extern "C" int user_initialize() { return 0; }
extern "C" void user_terminate() { return; }
extern "C" int __imp_getaddrinfo() { return 0; }
extern "C" int __imp_freeaddrinfo() { return 0; }
extern "C" int __imp_getnameinfo() { return 0; }


3.修改C++配置

在这里插入图片描述
其他,参考文章添加链接描述

4.新建wpf应用

在这里插入图片描述

5.编辑按钮点击事件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

namespace WpfApplication1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Fun();
        }

        public enum ProError
        {

            PRO_TK_NO_ERROR = 0,
            PRO_TK_GENERAL_ERROR = -1,
            PRO_TK_BAD_INPUTS = -2,
            PRO_TK_USER_ABORT = -3,
            PRO_TK_E_NOT_FOUND = -4,
            PRO_TK_E_FOUND = -5,
            PRO_TK_LINE_TOO_LONG = -6,
            PRO_TK_CONTINUE = -7,
            PRO_TK_BAD_CONTEXT = -8,
            PRO_TK_NOT_IMPLEMENTED = -9,
            PRO_TK_OUT_OF_MEMORY = -10,
            PRO_TK_COMM_ERROR = -11, /* communication error */
            PRO_TK_NO_CHANGE = -12,
            PRO_TK_SUPP_PARENTS = -13,
            PRO_TK_PICK_ABOVE = -14,
            PRO_TK_INVALID_DIR = -15,
            PRO_TK_INVALID_FILE = -16,
            PRO_TK_CANT_WRITE = -17,
            PRO_TK_INVALID_TYPE = -18,
            PRO_TK_INVALID_PTR = -19,
            PRO_TK_UNAV_SEC = -20,
            PRO_TK_INVALID_MATRIX = -21,
            PRO_TK_INVALID_NAME = -22,
            PRO_TK_NOT_EXIST = -23,
            PRO_TK_CANT_OPEN = -24,
            PRO_TK_ABORT = -25,
            PRO_TK_NOT_VALID = -26,
            PRO_TK_INVALID_ITEM = -27,
            PRO_TK_MSG_NOT_FOUND = -28,
            PRO_TK_MSG_NO_TRANS = -29,
            PRO_TK_MSG_FMT_ERROR = -30,
            PRO_TK_MSG_USER_QUIT = -31,
            PRO_TK_MSG_TOO_LONG = -32,
            PRO_TK_CANT_ACCESS = -33,
            PRO_TK_OBSOLETE_FUNC = -34,
            PRO_TK_NO_COORD_SYSTEM = -35,
            PRO_TK_E_AMBIGUOUS = -36,
            PRO_TK_E_DEADLOCK = -37,
            PRO_TK_E_BUSY = -38,
            PRO_TK_E_IN_USE = -39,
            PRO_TK_NO_LICENSE = -40,
            PRO_TK_BSPL_UNSUITABLE_DEGREE = -41,
            PRO_TK_BSPL_NON_STD_END_KNOTS = -42,
            PRO_TK_BSPL_MULTI_INNER_KNOTS = -43,
            PRO_TK_BAD_SRF_CRV = -44,
            PRO_TK_EMPTY = -45,
            PRO_TK_BAD_DIM_ATTACH = -46,
            PRO_TK_NOT_DISPLAYED = -47,
            PRO_TK_CANT_MODIFY = -48,
            PRO_TK_CHECKOUT_CONFLICT = -49,
            PRO_TK_CRE_VIEW_BAD_SHEET = -50,
            PRO_TK_CRE_VIEW_BAD_MODEL = -51,
            PRO_TK_CRE_VIEW_BAD_PARENT = -52,
            PRO_TK_CRE_VIEW_BAD_TYPE = -53,
            PRO_TK_CRE_VIEW_BAD_EXPLODE = -54,
            PRO_TK_UNATTACHED_FEATS = -55,
            PRO_TK_REGEN_AGAIN = -56,
            PRO_TK_DWGCREATE_ERRORS = -57,
            PRO_TK_UNSUPPORTED = -58,
            PRO_TK_NO_PERMISSION = -59,
            PRO_TK_AUTHENTICATION_FAILURE = -60,
            PRO_TK_OUTDATED = -61,
            PRO_TK_INCOMPLETE = -62,
            PRO_TK_CHECK_OMITTED = -63,
            PRO_TK_MAX_LIMIT_REACHED = -64,
            PRO_TK_OUT_OF_RANGE = -65,
            PRO_TK_CHECK_LAST_ERROR = -66,
            /*
              NOTE: PRO_TK_NO_PLM_LICENSE error added to handle lack of PTC Mechanical Design I/II licenses
            */
            PRO_TK_NO_PLM_LICENSE = -67,
            PRO_TK_INCOMPLETE_TESS = -68,

            /*
              NOTE: the errors below are reserved for the Creo Toolkit API. Applications
              should never return these errors.
            */
            PRO_TK_APP_CREO_BARRED = -88,
            PRO_TK_APP_TOO_OLD = -89,
            PRO_TK_APP_BAD_DATAPATH = -90,
            PRO_TK_APP_BAD_ENCODING = -91,
            PRO_TK_APP_NO_LICENSE = -92,
            PRO_TK_APP_XS_CALLBACKS = -93,
            PRO_TK_APP_STARTUP_FAIL = -94,
            PRO_TK_APP_INIT_FAIL = -95,
            PRO_TK_APP_VERSION_MISMATCH = -96,
            PRO_TK_APP_COMM_FAILURE = -97,
            PRO_TK_APP_NEW_VERSION = -98,
            PRO_TK_APP_UNLOCK = -99,
            PRO_TK_APP_JLINK_NOT_ALLOWED = -100


        }

        public enum ProMdlType
        {
            PRO_MDL_UNUSED = -1,
            PRO_MDL_ASSEMBLY = 1,
            PRO_MDL_PART = 2,
            PRO_MDL_DRAWING = 4,
            PRO_MDL_3DSECTION = 7,
            PRO_MDL_2DSECTION = 11,
            PRO_MDL_LAYOUT = 19,     /* (*.lay file)  Notebook model.
                                           Formerly known as Layout model */
            PRO_MDL_DWGFORM = 33,
            PRO_MDL_MFG = 37,
            PRO_MDL_REPORT = 105,
            PRO_MDL_MARKUP = 116,
            PRO_MDL_DIAGRAM = 121,
            PRO_MDL_CE_SOLID = 316,   /* Layout model; NOTE: this type is read-only, 
                                           passing it back to Creo may lead to unpredictable behavior */
            PRO_MDL_CE_DRAWING = 315, /* reserved for internal use */
            PRO_MDL_DRW_SOLID = 319   /* reserved for internal use */

        }

        [DllImport("Creo.Protk.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
        public static extern ProMdlType MdlRetrieve(string path,ProMdlType type,out IntPtr mdl);

        [DllImport("Creo.Protk.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
        public static extern int Sub(int a,int b);

        public static void Fun()
        {
            try
            {
                ProError err;
                IntPtr mdl = IntPtr.Zero;
                string name = "prt001";
                //err = MdlRetrieve(name);
                ProMdlType type = MdlRetrieve(name,ProMdlType.PRO_MDL_PART,out mdl);
                //MessageBox.Show(Sub(1, 2).ToString());
            }
            catch (Exception ex)
            {
                Console.Write(ex);
            }

        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值