使用java语言二次开发一键重排刀号源代码
code by 郭祥,2025-10-05 武汉本代码使用java语言编写,JDK版本为20,需要设置环境变量,要使用本代码需要
有编程功底,因为相关的机床刀具号定义文件文本格式,以及如何辨别工序指派的机床类型本人不会耐心解答
本代码实现了,机床固定刀号锁定,其他刀号指定刀号范围进行重新排刀号,分机床以实现不同机床的刀号调整
以下是源代码内容,由本人编写,转载需注明出处
import nxopen.*;
import nxopen.cam.*;
import nxopen.mechatronics.CamBuilder;
import java.io.*;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
public class SetMacine {
public static void main(String [] args) throws NXException, java.rmi.RemoteException {
//取得UG得 Session,标准步骤
Session theSession = (Session) SessionFactory.get("Session");
//取得工作部件
Part workPart = theSession.parts().work();
//取得部件得显示
Part displayPart = theSession.parts().display();
//获取UG得图像窗口session
UI theUI = (UI) SessionFactory.get("UI");
//取得当前选中的目标
CAMObject targ = (CAMObject) theUI.selectionManager().getSelectedTaggedObject(0);
if(workPart.camsetup().isGroup(targ)){
NCGroup group = (NCGroup) targ;
String group_name = group.name();
//String[] farmat_name = group_name.split("-");
theSession.listingWindow().open();
theSession.listingWindow().writeLine("程序组名称= "+group_name);
//String miaoshu = group.builderProperties(); //读取创建信息
//theSession.listingWindow().writeLine("程序组信息= "+miaoshu);
//String[] qiege = miaoshu.split("\"");
//for(String t:qiege){
// theSession.listingWindow().writeLine("分割输出="+t);
// }
//String Description = qiege;
//theSession.listingWindow().writeLine("Description"+Description);//输出描述字符,但是描述字符只在UG启动时读取,无法做为参照
String operatio="";
nxopen.cam.ProgramOrderGroupBuilder programOrderGroupBuilder1;
programOrderGroupBuilder1 = workPart.camsetup().camgroupCollection().createProgramOrderGroupBuilder(group);
nxopen.TaggedObject taggedObject2;
taggedObject2 = programOrderGroupBuilder1.startUdeSet().udeList().findItem(0);
nxopen.cam.Ude ude1 = ((nxopen.cam.Ude)taggedObject2);
nxopen.cam.UdeParameter udeParameter1;
udeParameter1 = ude1.getParameter("operator_message"); //获取程序组中的操作员信息
operatio += udeParameter1.stringText();
// operatio=group.getStringValue("operator_message");
theSession.listingWindow().writeLine("操作员信息="+operatio);
String env = System.getenv("TOOL_UPON");
//theSession.listingWindow().writeLine("TOOL_UPON="+env);
//String path ="D:\\BaiduSyncdisk\\TOOL_UPON\\tool_config.xlsx";
String path = env+"\\"+operatio+".txt";
File testfile = new File(path);
if(!testfile.exists()){ //如果文件路径下文件没有,就使用默认路径进行
path = env+"\\"+"DEFAULT"+".txt";
}
theSession.listingWindow().writeLine(path);
//映射表缓存,下面的循环会将文件上的映射表循环读取到其中
ArrayList<DataStruct> list = new ArrayList<>();
//刀具号占用情况注册表
ArrayList<DataStruct> toolNumberRegedit = new ArrayList<>();
try {
File file = new File(path);
if(file.exists()){
// FileReader fr = new FileReader(file);
//处理ANSI格式的编码文件
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path),"GB2312"));
String line;
while ((line = br.readLine()) != null) {
// 处理每行数据
// System.out.println(line);
//theSession.listingWindow().writeLine(line);
String[] text = line.split("\\|");
DataStruct data = new SetMacine.DataStruct();
data.number = Integer.parseInt(text);
data.name = text;
data.lenght_number = Integer.parseInt(text);
data.dia_number = Integer.parseInt(text);
if(text.equals("锁定刀号"))
{
data.reorder=false;
}else{
data.reorder = true;//可重排
}
data.lowNumber=Integer.parseInt(text); //
data.higNumber=Integer.parseInt(text);
list.add(data);
}
br.close();
//fr.close();
//检查刀具映射
checkToolUpon(workPart,list,group,toolNumberRegedit);
}else{
theUI.nxmessageBox().show("Block Styler",
NXMessageBox.DialogType.WARNING,
"在指定路径:“"+path+"”下未发现机床配置表文件!");
}
} catch (IOException e) {
e.printStackTrace();
}
//打印读取到的映射表
//for(DataStruct ds:list){
// theSession.listingWindow().writeLine("刀号:"+ ds.number +"刀名:"+ ds.name + "刀长号:"+ ds.lenght_number + "刀经号:"+ ds.dia_number);
// }
}else{
theUI.nxmessageBox().show("Block Styler",
NXMessageBox.DialogType.WARNING,
"选中的目标不是程序组");
}
}
public static final int getUnloadOption() { return BaseSession.LibraryUnloadOption.IMMEDIATELY; }
接上贴,代码长度限制未发完全,以下是剩下部分
/**
*
* @param workPart
* @param list
* @param ncGroup
* @param toolNumberRegedit
* @throws NXException
* @throws RemoteException
*/
public static void checkToolUpon(Part workPart ,
ArrayList<DataStruct> list,
NCGroup ncGroup,
ArrayList<DataStruct> toolNumberRegedit
) throws NXException, RemoteException {
//取得UG得 Session,标准步骤
Session theSession = (Session) SessionFactory.get("Session");
//循环检查程序组下面的操作是否可以映射
CAMObject[] cam_list= ncGroup.getMembers();
for(CAMObject cam:cam_list){
if(workPart.camsetup().isOperation(cam)){
Operation op = (Operation)cam;
AddToolNumberRegedit(op,toolNumberRegedit); //向注册列表添加数据
//取得UG得 Session,标准步骤
// Session theSession = (Session) SessionFactory.get("Session");
NCGroup ncg = (NCGroup) op.getParent(CAMSetup.View.MACHINE_TOOL);
//Tool tool = (Tool)ncg;
//tool.
//theSession.listingWindow().writeLine("刀具名字="+ncg.name());//打印工序的刀具名称
for(DataStruct ds:list){
if(ds.name.equals(ncg.name())){
if(ds.reorder){//支持重排刀号
//theSession.listingWindow().writeLine("重排刀具号");
if(checkToolNumber(ds,toolNumberRegedit)) { //检查目前的刀号是否重复,不重复不需要重排
for (int i = ds.lowNumber; i <= ds.higNumber; i++) {
ds.number = i;
if (checkToolNumber(ds, toolNumberRegedit)) {
//theSession.listingWindow().writeLine("刀具号被占用,刀具名:" + ds.name + "刀具号:" + ds.number);//
} else {
ds.lenght_number = i;
ds.dia_number = i;
//theSession.listingWindow().writeLine("新刀具号,刀具名:" + ds.name + "刀具号:" + ds.number);
break;
}
}
}
AddToolNumberRegedit(ds,toolNumberRegedit); //向注册列表添加数据
}
//theSession.listingWindow().writeLine(ncg.name()+"与列表的刀具"+ ds.name+"相等");
OperationBuilder cb = workPart.camsetup().camoperationCollection().createBuilder(op);
//cb.toolChangeSetting().setTextValue(); //设置刀具名称
cb.toolChangeSetting().number().setValue(ds.number);//替换刀号
// cb.toolChangeSetting().zoffset().setValue(ds.lenght_number);//这是Z轴偏置\
// cb.toolChangeSetting().holderNumber().setValue(ds.lenght_number); //未知参数
cb.toolChangeSetting().adjustRegister().setValue(ds.lenght_number); //替换刀长补正
cb.commit();
}
}
}else{//如果是一个内置的子程序组
if(workPart.camsetup().isGroup(cam)){
checkToolUpon(workPart,list,(NCGroup)cam,toolNumberRegedit);
}
}
}
}
/**
* 向刀具号注册表填充数据
* @param op
* @param regeditList
* @return
*/
public static DataStruct AddToolNumberRegedit(Operation op,ArrayList<DataStruct> regeditList){
DataStruct ds = new DataStruct();
try {
//取得UG得 Session,标准步骤
Session theSession = (Session) SessionFactory.get("Session");
//取得工作部件
Part workPart = theSession.parts().work();
NCGroup ncg = (NCGroup) op.getParent(CAMSetup.View.MACHINE_TOOL);
ds.name = ncg.name(); //刀具名称
OperationBuilder cb = workPart.camsetup().camoperationCollection().createBuilder(op);
ds.number=cb.toolChangeSetting().number().value();
ds.lenght_number=cb.toolChangeSetting().adjustRegister().value();//刀长
ds.dia_number = cb.toolChangeSetting().cutcomRegister().value(); //刀经
regeditList.add(ds);
return ds;
} catch (NXException e) {
throw new RuntimeException(e);
} catch (RemoteException e) {
throw new RuntimeException(e);
}
}
/**
*
* @param ds
* @param regeditList
* @return
*/
public static DataStruct AddToolNumberRegedit(DataStruct ds,ArrayList<DataStruct> regeditList){
regeditList.add(ds);
return ds;
}
/**
* 检查刀具是否在占用表中
* @param op
* @param regeditList
* @return
*/
public static boolean checkToolNumber(Operation op,ArrayList<DataStruct> regeditList){
try {
//取得UG得 Session,标准步骤
Session theSession = (Session) SessionFactory.get("Session");
//取得工作部件
Part workPart = theSession.parts().work();
NCGroup ncg = (NCGroup) op.getParent(CAMSetup.View.MACHINE_TOOL);
OperationBuilder cb = workPart.camsetup().camoperationCollection().createBuilder(op);
String name = ncg.name();
int number=cb.toolChangeSetting().number().value();
int lenght_number=cb.toolChangeSetting().adjustRegister().value();//刀长
int dia_number = cb.toolChangeSetting().cutcomRegister().value(); //刀经
for(DataStruct dds:regeditList){
if(number==dds.number){ //刀具号码一样
if(name.equals(dds.name)){
// return false; //刀号一样,名称一样,不视为占用,继续进行循环
}else{
return true;//刀号一样,名称不同,视为占用
}
}
}
return false;
} catch (NXException e) {
throw new RuntimeException(e);
} catch (RemoteException e) {
throw new RuntimeException(e);
}
}
/**
* 检查刀具是否在占用表中
* @param ds
* @param regeditList
* @return
*/
public static boolean checkToolNumber(DataStruct ds,ArrayList<DataStruct> regeditList){
int number=ds.number;
int lenght_number=ds.lenght_number;//刀长
int dia_number = ds.dia_number; //刀经
for(DataStruct dds:regeditList){
if(number==dds.number){ //刀具号码一样
if(ds.name.equals(dds.name)){
// return false; //刀号一样,名称一样,不视为占用,继续进行循环
}else{
return true;//刀号一样,名称不同,视为占用
}
}
}
return false;
}
public static class DataStruct{
/**
* 刀具号码
*/
public int number;//
/**
* 刀具名称
*/
public String name;
/**
* 刀长补正号
*/
public int lenght_number;
/**
* 刀经补正号
*/
public int dia_number;
/**
* 当刀具号码已被占用时,是否可以对刀具进行重新排序
* true= 可重新排序
* false = 不可重新进行排序,相当于锁定刀号
*/
public boolean reorder;//是否可重新排序
/**
* 重排序可用刀号低范围
*/
public int lowNumber;
/**
* 重排序可用刀号高范围
*/
public int higNumber;
public DataStruct(){
name="";
number=0;
lenght_number = 0;
dia_number = 0;
reorder = false; //默认不可以重新排序
lowNumber = 0;
higNumber = 0;
}
}
}
666666666666666666666666 都怪读书时不好好学习,学不会这么高大上的东西,只能看看后评论一句“牛”
页:
[1]