📄 stepncmodel.java
字号:
/* $RCSfile: coolant.cxx,v $
* $Revision: 1.3 $ $Date: 2006/08/21 14:15:51 $
*
* Copyright (c) 1991-2006 by STEP Tools Inc.
* All Rights Reserved.
*
* This file may be distributed and/or modified under the terms of
* the GNU General Public License version 2 as published by the Free
* Software Foundation and appearing in the file LICENSE.GPL included
* with this file.
*
* THIS FILE IS PROVIDED "AS IS" WITH NO WARRANTY OF ANY KIND,
* INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE.
*
* ----------------------------------------
*/
package com.steptools.view238;
import java.io.*;
import java.util.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
/**
* AP238 Processing class. An instance of this class provides read access to
* the data in an AP238 (P28/XML) file. This includes functionality to find
* and access the toolpath objects in the design
*/
public class StepNcModel {
/** URI for AP238 namespace */
final static String INSTANCE_URI = "urn:oid:1.0.10303.238.1.0.1";
/**
* Get the root element for an XML file
* @param file the file to read
* @return the root element of the file
*/
private static Element readXMLFile (File file)
throws IOException, ParserConfigurationException, SAXException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder parser = dbf.newDocumentBuilder();
Document doc = parser.parse(file);
return doc.getDocumentElement();
}
private Element root;
private Map<String,Element> instances = new HashMap<String,Element>();
/** Get the value of a STEP attribute that is written as a contained
* element. This encoding scheme is used for SELECTs and aggregates
* (although the aggregates could have multiple values, so this is of
* dubious value in that case.
* @param el The DOM element to search for a child value in.
* @return The value of the ref attribute.
*/
public String getChildRef(Element el) {
NodeList nl = el.getChildNodes();
int sz = nl.getLength();
for (int i=0; i<sz; i++) {
Node n = nl.item(i);
if (!(n instanceof Element))
continue;
Element ch_el = (Element)n;
if (!(INSTANCE_URI.equals(el.getNamespaceURI())))
continue;
String ref = ch_el.getAttribute ("ref");
return ref;
}
return null;
}
/**
* Get the value of a STEP attribute in a specified object.
* @param obj the ID for the object being queried.
* @param prop the name of the property to obtain.
* @return the value of the property as a string. For STEP objects, this
* is an ID for the value.
*/
public String getAttribute(String obj, String prop) {
Element el = instances.get(obj);
if (el.hasAttribute(prop))
return el.getAttribute(prop);
NodeList nl = el.getChildNodes();
int sz = nl.getLength();
for (int i=0; i<sz; i++) {
Node n = nl.item(i);
if (!(n instanceof Element))
continue;
Element ch_el = (Element)n;
if (!(INSTANCE_URI.equals(ch_el.getNamespaceURI())))
continue;
if (!(ch_el.getLocalName().equals(prop)))
continue;
return getChildRef(ch_el);
}
return null;
}
/**
* Get the value of a STEP aggregate-typed attribute in a specified object.
* @param obj the ID for the object being queried.
* @param prop the name of the property to obtain.
* @return an array of values for value of the property. For STEP objects,
* this is an ID for the value.
*/
public String[] getAttributeCollection(String obj, String prop) {
Element el = instances.get(obj);
if (el.hasAttribute(prop)) {
return el.getAttribute(prop).split("\\s");
}
// NodeList nl = el.getChildNodes();
// int sz = nl.getLength();
// for (int i=0; i<sz; i++) {
// Node n = nl.item(i);
// if (!(n instanceof Element))
// continue;
// Element ch_el = (Element)n;
// if (!(INSTANCE_URI.equals(ch_el.getNamespaceURI())))
// continue;
// if (!(ch_el.getLocalName().equals(prop)))
// continue;
// return getChildRef(ch_el);
// }
return null;
}
/** Abstract base type for a ToolPath */
public abstract class Curve {}
/** A single line segment in a ToolPath */
public class LineSegment extends Curve {
double x1;
double y1;
double z1;
double x2;
double y2;
double z2;
}
/** A single arc in a ToolPath */
public class Arc extends Curve {
double radius;
boolean sense;
/** Center */
double ox;
double oy;
double oz;
/** Axis direction */
double ax;
double ay;
double az;
/** First trim point */
double tx1;
double ty1;
double tz1;
/** Second trim point */
double tx2;
double ty2;
double tz2;
}
/** A single tool path. */
public class ToolPath {
final String toolpath_id;
final String curve_id;
final List<Curve> curves = new ArrayList<Curve>();
ToolPath (String tp, String c) {
toolpath_id = tp;
curve_id = c;
}
public String getName() {
return getAttribute(toolpath_id, "Name");
}
}
List<ToolPath> toolpaths = new ArrayList<ToolPath> ();
/** Get the STEP entity type on an object */
public String getType (String id) {
Element el = instances.get(id);
return el.getLocalName();
}
private void appendPolyline (ToolPath tp, String pl) {
String[] points = getAttributeCollection(pl, "Points");
double oldx=0.0, oldy=0.0, oldz=0.0;
boolean first = true;
for (String pid : points) {
String[] coords = getAttributeCollection(pid, "Coordinates");
double x = parseDouble(coords[0]);
double y = parseDouble(coords[1]);
double z = parseDouble(coords[2]);
if (first)
first = false;
else {
LineSegment seg = new LineSegment();
seg.x1 = oldx;
seg.y1 = oldy;
seg.z1 = oldz;
seg.x2 = x;
seg.y2 = y;
seg.z2 = z;
tp.curves.add(seg);
}
oldx = x;
oldy = y;
oldz = z;
}
}
static double parseDouble(String txt) {
if (txt.contains("..")) {
System.out.println ("WARNING: corrupt real number: "+txt);
txt = txt.replaceAll("\\.+", ".");
System.out.println ("Fixed to "+txt);
}
return Double.parseDouble(txt);
}
private void appendTrimmed_curve(ToolPath tp, String tc) {
String trim1 = getAttribute(tc, "Trim_1");
if ( !("Cartesian_point".equals(getType(trim1)))) {
System.out.println ("Unexpected type for trim1 "+ getType(trim1));
return;
}
String trim2 = getAttribute(tc, "Trim_2");
if ( !("Cartesian_point".equals(getType(trim2)))) {
System.out.println ("Unexpected type for trim2 "+ getType(trim2));
return;
}
String basis = getAttribute(tc, "Basis_curve");
if ( !("Circle".equals(getType(basis)))) {
System.out.println ("Unexpected type for basis_curve "
+ getType(basis));
return;
}
String pos = getAttribute(basis, "Position");
if ( !("Axis2_placement_3d".equals(getType(pos)))) {
System.out.println ("Unexpected type for position "+ getType(pos));
return;
}
String center = getAttribute(pos, "Location");
String[] center_coords = getAttributeCollection(center, "Coordinates");
String axis = getAttribute(pos, "Axis");
String[] axis_comps = getAttributeCollection(axis, "Direction_ratios");
// String dir = getAttributeCollection(pos, "Ref_direction");
// String[] dir_comps = getAttributeCollection(dir, "Direction_ratios");
Arc arc = new Arc();
tp.curves.add(arc);
arc.radius = parseDouble(getAttribute(basis, "Radius"));
arc.sense = "true".equals(getAttribute(tc, "Sense_agreement"));
arc.ox = parseDouble(center_coords[0]);
arc.oy = parseDouble(center_coords[1]);
arc.oz = parseDouble(center_coords[2]);
arc.ax = parseDouble(axis_comps[0]);
arc.ay = parseDouble(axis_comps[1]);
arc.az = parseDouble(axis_comps[2]);
String[] trim1_coords = getAttributeCollection(trim1, "Coordinates");
arc.tx1 = parseDouble(trim1_coords[0]);
arc.ty1 = parseDouble(trim1_coords[1]);
arc.tz1 = parseDouble(trim1_coords[2]);
String[] trim2_coords = getAttributeCollection(trim2, "Coordinates");
arc.tx2 = parseDouble(trim2_coords[0]);
arc.ty2 = parseDouble(trim2_coords[1]);
arc.tz2 = parseDouble(trim2_coords[2]);
}
private void appendCompositeCurve(ToolPath tp, String pl) {
String[] segments = getAttributeCollection(pl, "Segments");
for (String seg : segments) {
String parent = getAttribute(seg, "Parent_curve");
String type = getType(parent);
if (type.equals("Trimmed_curve")) {
appendTrimmed_curve(tp, parent);
}
else if (type.equals("Polyline")) {
appendPolyline(tp, parent);
}
else {
System.out.println (" Unexpected type \"" +type+
"\" in composite" +parent);
}
}
}
private ToolPath createToolPath(String tp, String curve_id) {
ToolPath ret = new ToolPath(tp, curve_id);
String type = getType(curve_id);
if (type.equals("Polyline")) {
appendPolyline(ret, curve_id);
}
else if (type.equals("Composite_curve")) {
appendCompositeCurve(ret, curve_id);
}
return ret;
}
private void findBasicCurve(String apr) {
String ap = getAttribute(apr, "Property");
if (ap == null)
return;
if (!(getAttribute(ap, "Name").equals("basic curve")))
return;
String tp = getAttribute(ap, "Definition");
if (!("Machining_toolpath".equals(getType(tp))))
return;
String rep = getAttribute(apr, "Representation");
if (rep == null)
return;
for (String item : getAttributeCollection(rep, "Items")) {
String type = getType(item);
if (type.equals("Polyline")
|| type.equals("Composite_curve")) {
String tp_name = getAttribute(tp, "Name");
toolpaths.add(createToolPath(tp, item));
break;
}
}
}
/**
* Display a toolpath for debugging
*/
public void dump(PrintStream out, ToolPath tp) {
out.println ("Toolpath: "+tp.getName());
for (Curve c : tp.curves) {
if (c instanceof LineSegment) {
LineSegment s = (LineSegment) c;
System.out.println
(" Segment: ("
+s.x1+ ", " +s.y1+ ", " +s.z1+ ") -> ("
+s.x2+ ", " +s.y2+ ", " +s.z2+ ")");
}
else if (c instanceof Arc) {
Arc a = (Arc) c;
System.out.println
(" Arc: O=("+a.ox+ ", " +a.oy+ ", " +a.oz+ "), "
+" r="+a.radius+ " Sense=" +a.sense);
System.out.println
(" Axis dir = ( " +a.ax+ ", " +a.ay+ ", " +a.az+") ");
System.out.println
(" Range: ("
+a.tx1+ ", " +a.ty1+ ", " +a.tz1+ ") -> ("
+a.tx2+ ", " +a.ty2+ ", " +a.tz2+ ")");
}
else
throw new RuntimeException ("Unexpected case");
}
}
/**
* Create a model given a AP238 XML file
* @param ap238 the file to read.
* @throws IOException when there is an IO exception processing the file.
* @throws StepNcException general problem processing the data.
*/
public StepNcModel(File ap238) throws IOException, StepNcException {
try {
root = readXMLFile(ap238);
} catch (ParserConfigurationException ex) {
throw new StepNcException (ex);
} catch (SAXException ex) {
throw new StepNcException (ex);
}
NodeList nl = root.getChildNodes();
int sz = nl.getLength();
Set<String> aprs = new LinkedHashSet<String>();
for (int i=0; i<sz; i++) {
Node n = nl.item(i);
if (!(n instanceof Element))
continue;
Element el = (Element)n;
if (!(INSTANCE_URI.equals(el.getNamespaceURI())))
continue;
String id = el.getAttribute ("id");
instances.put(id, el);
if (el.getLocalName().equals("Action_property_representation")) {
aprs.add(id);
}
}
/* Get the basiccurves */
for (String apr : aprs) {
findBasicCurve(apr);
}
}
/**
* Sample driver program to display the AP238 data in a file
*/
public static void main(String[] argv) throws IOException, StepNcException{
File file = new File(argv[0]);
StepNcModel mod = new StepNcModel(file);
for (ToolPath tp : mod.toolpaths) {
mod.dump(System.out, tp);
System.out.println();
}
}
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -