Showing results for 
Search instead for 
Do you mean 
Reply

Java: cutting needed volume

[ Edited ]

Hello!

 

I am doing my diploma. Theme: finding the displacement of a sea craft.

 

The current task is: find a datum plane, that cuts from the assembly specified volume.

Datum is parallel to XY.

 

I do it with looping nxopen.features.SplitBodyBuilder trying datums with step.

 

But i have a problem with selecting spitted body: code returns always the smalles body. 

 

I know there is Space Finder dialog box in NX, but it's not journaled. 

 

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Arrays;

import nxopen.*;
import nxopen.Point3d;
import nxopen.geometricutilities.BooleanToolBuilder;
import nxroutine.*;

public class ShipStabilityAnalysis {
	
	nxopen.Part workPart;
	nxopen.Session theSession;
	nxopen.UFSession theUFSession;
	nxopen.Body[] originalBds;
	
	ShipStabilityAnalysis() throws NXException, RemoteException
	{
		theSession = (nxopen.Session)nxopen.SessionFactory.get("Session");
		workPart = theSession.parts().work();
		theUFSession = (UFSession)SessionFactory.get("UFSession");
		originalBds = Misc.BodyListToBodyArr(Misc.BodyColltoBodyList(workPart.bodies()));
	}
	
	void findWaterline() throws RemoteException, NXException
	{
	    theUFSession.ui().openListingWindow();
		nxopen.DatumPlane datumPlane = null;
		nxopen.Body disp = null;
		nxopen.MeasureBodies mbDisp = null;
		nxopen.MeasureBodies mbAll = null;
		//find mass of whole ship
		mbAll = MassMeasure.measureBodies(Misc.BodyColltoBodyList(workPart.bodies()));
		//check if this ship can float
		if( mbAll.volume() * Constants.seaWaterDensity < mbAll.mass())
		{
			theUFSession.ui().writeListingWindow("\nship will sink: ");
			theUFSession.ui().writeListingWindow("\nvolume of ship: " + Double.toString(mbAll.volume()));
			theUFSession.ui().writeListingWindow("\nsea Water Density: " + Double.toString(Constants.seaWaterDensity));
			theUFSession.ui().writeListingWindow("\nmass of ship: " + Double.toString(mbAll.mass()) +
					MassMeasure.kilo);
			theUFSession.ui().writeListingWindow("\ndisplacemnt : " + Double.toString(mbAll.volume() * Constants.seaWaterDensity) +
					MassMeasure.kilo);
			return;
		}
		//variable for loop
		double targetDisp = mbAll.mass() / Constants.seaWaterDensity;
		double toler = targetDisp * 0.05;
		double step = 10;
		double top = 500;
		double bot = 5;
		
		theUFSession.ui().writeListingWindow("\nvolume of ship: " + Double.toString(mbAll.volume()));
		theUFSession.ui().writeListingWindow("\nsea Water Density: " + Double.toString(Constants.seaWaterDensity));
		theUFSession.ui().writeListingWindow("\nmass of ship: " + Double.toString(mbAll.mass()) +
				MassMeasure.kilo);
		theUFSession.ui().writeListingWindow("\ndisplacemnt : " + Double.toString(mbAll.volume() * Constants.seaWaterDensity) +
				MassMeasure.kilo);
		theUFSession.ui().writeListingWindow("\ntarget volume of displacement: " + Double.toString(targetDisp) +
				MassMeasure.mm3);
		//LOOP splitting right displacement
		for(double i = bot; i < top; i += step)
		{
			//TOOL: try a datum
			int mark = theSession.setUndoMark(nxopen.Session.MarkVisibility.INVISIBLE, "null");
			datumPlane = nxroutine.Plane.CreateDatumPlane
					(workPart, new nxopen.Point3d(0, 0, i), new nxopen.Vector3d(0.0, 0.0, 1.0));
			//Find displacement with dat datum
			disp = this.findDisplacement(datumPlane);
			
			findSplitted();
//			break;
			
			//check volume
			mbDisp = MassMeasure.measureBodies(disp);
//			theUFSession.ui().writeListingWindow("\n");
//			theUFSession.ui().writeListingWindow(Double.toString(mbDisp.volume()));
			
			if((Math.abs(mbDisp.volume() - targetDisp) <= toler)
					|| (Math.abs((mbAll.volume() - mbDisp.volume()) - targetDisp) <= toler))
				break;
			else
				theSession.undoToMark(mark, null);
		}
	}
	
	void findSplitted() throws NXException, java.rmi.RemoteException
	{
		nxopen.Body[] bds = Misc.BodyListToBodyArr(Misc.BodyColltoBodyList(workPart.bodies()));
		nxopen.Body[] condidates = Misc.subtract(bds, originalBds);
		
		for(nxopen.Body b : condidates)
			b.highlight();
		double h;
		for(int i = 0; i < 1e7; i++)
			h = i * i /6.4;
		
//		theUFSession.ui().writeListingWindow("\nbefore\n");
//		theUFSession.ui().writeListingWindow(Arrays.toString(originalBds));
//		theUFSession.ui().writeListingWindow("\nafter\n");
//		theUFSession.ui().writeListingWindow(Arrays.toString(bds));
	}
	
	nxopen.Body findDisplacement(nxopen.DatumPlane datumPlane) throws NXException, java.rmi.RemoteException
	{
		nxopen.features.SplitBodyBuilder splitBodyBuilder;
	    splitBodyBuilder = workPart.features().createSplitBodyBuilder(null);
	    
//	    nxopen.UFSession theUFSession = (UFSession)SessionFactory.get("UFSession");
//	    theUFSession.ui().openListingWindow();
	    //TOOL
	    //nxopen.DatumPlane datumPlane = nxroutine.Plane.CreateDatumPlane(workPart, null, null);
	
	    
	    nxopen.FaceDumbRule faceDumbRule;
	    faceDumbRule = workPart.scRuleFactory().createRuleFaceDatum(new nxopen.DatumPlane[] {datumPlane});
	    //add plane
	    splitBodyBuilder.booleanTool().facePlaneTool().toolFaces().faceCollector().replaceRules(
	    			new nxopen.SelectionIntentRule[]{faceDumbRule}, false);
	    splitBodyBuilder.booleanTool().setToolOption(BooleanToolBuilder.BooleanToolType.FACE_OR_PLANE);
	    
	    
	    //TARGET
	    
	    //nxopen.Body[] bds = Misc.BodyListToBodyArr(Misc.findAllBodies(workPart.componentAssembly().rootComponent()));
	    nxopen.Body[] bds = Misc.BodyListToBodyArr(Misc.BodyColltoBodyList(workPart.bodies()));
	    
//	    theUFSession.ui().writeListingWindow(Arrays.toString(bds));
	    
	    splitBodyBuilder.setTargetBodyCollector(Misc.select(workPart, bds));

	    //COMMIT
	    splitBodyBuilder.commit();
	    
//	    theUFSession.ui().writeListingWindow("\n***\n");
	    bds = Misc.BodyListToBodyArr(Misc.BodyColltoBodyList(workPart.bodies()));
//	    theUFSession.ui().writeListingWindow(Arrays.toString(bds));
	    
	    return bds[bds.length-1];
	    
	    
	    //196349.540849362 mm2
	    
	}
	public static void main(String[] args) throws RemoteException, NXException 
	{
		
	}
}