@@ -23,6 +23,7 @@ This file is part of Universal Gcode Sender (UGS).
2323import com .willwinder .ugs .nbp .designer .io .eagle .EaglePnpReader ;
2424import com .willwinder .ugs .nbp .designer .io .kicad .KiCadPosReader ;
2525import com .willwinder .ugs .nbp .designer .io .svg .SvgReader ;
26+ import com .willwinder .ugs .nbp .designer .io .ugsd .UgsDesignReader ;
2627import com .willwinder .ugs .nbp .designer .logic .Controller ;
2728import com .willwinder .ugs .nbp .designer .logic .ControllerFactory ;
2829import com .willwinder .ugs .nbp .designer .logic .Tool ;
@@ -41,6 +42,7 @@ This file is part of Universal Gcode Sender (UGS).
4142import javax .swing .filechooser .FileNameExtensionFilter ;
4243import java .awt .event .ActionEvent ;
4344import java .io .File ;
45+ import java .util .Arrays ;
4446import java .util .Optional ;
4547
4648/**
@@ -54,6 +56,14 @@ This file is part of Universal Gcode Sender (UGS).
5456 displayName = "Import file" ,
5557 lazy = false )
5658public final class ToolImportAction extends AbstractDesignAction {
59+ public static final FileNameExtensionFilter [] FILE_NAME_EXTENSION_FILTERS = new FileNameExtensionFilter []{
60+ new FileNameExtensionFilter ("Scalable Vector Graphics (.svg)" , "svg" ),
61+ new FileNameExtensionFilter ("Autodesk CAD (.dxf)" , "dxf" ),
62+ new FileNameExtensionFilter ("Carbide Create (.c2d)" , "c2d" ),
63+ new FileNameExtensionFilter ("Eagle (.mnt, .mnb)" , "mnt" , "mnb" ),
64+ new FileNameExtensionFilter ("KiCad (.pos)" , "pos" ),
65+ new FileNameExtensionFilter ("UGS design (.ugsd)" , "ugsd" )
66+ };
5767
5868 public static final String SMALL_ICON_PATH = "img/import.svg" ;
5969 public static final String LARGE_ICON_PATH = "img/import24.svg" ;
@@ -68,51 +78,53 @@ public ToolImportAction() {
6878 this .controller = ControllerFactory .getController ();
6979 }
7080
81+ public static void readDesign (Controller controller , BackendAPI backend , File f ) {
82+ Optional <Design > optionalDesign = Optional .empty ();
83+ if (StringUtils .endsWithIgnoreCase (f .getName (), ".svg" )) {
84+ SvgReader svgReader = new SvgReader ();
85+ optionalDesign = svgReader .read (f );
86+ } else if (StringUtils .endsWithIgnoreCase (f .getName (), ".dxf" )) {
87+ DxfReader reader = new DxfReader (backend .getSettings ());
88+ optionalDesign = reader .read (f );
89+ } else if (StringUtils .endsWithIgnoreCase (f .getName (), ".c2d" )) {
90+ C2dReader reader = new C2dReader ();
91+ optionalDesign = reader .read (f );
92+ } else if (StringUtils .endsWithIgnoreCase (f .getName (), ".mnt" ) ||
93+ StringUtils .endsWithIgnoreCase (f .getName (), ".mnb" )) {
94+ EaglePnpReader reader = new EaglePnpReader ();
95+ optionalDesign = reader .read (f );
96+ } else if (StringUtils .endsWithIgnoreCase (f .getName (), ".pos" )) {
97+ KiCadPosReader reader = new KiCadPosReader ();
98+ optionalDesign = reader .read (f );
99+ } else if (StringUtils .endsWithIgnoreCase (f .getName (), ".ugsd" )) {
100+ UgsDesignReader reader = new UgsDesignReader ();
101+ optionalDesign = reader .read (f );
102+ }
103+
104+ if (optionalDesign .isPresent ()) {
105+ Design design = optionalDesign .get ();
106+ controller .setTool (Tool .SELECT );
107+ controller .addEntities (design .getEntities ());
108+ controller .getSelectionManager ().addSelection (design .getEntities ());
109+ controller .getDrawing ().repaint ();
110+ } else {
111+ throw new RuntimeException ("Could not open: " + f .getName ());
112+ }
113+ }
114+
71115 @ Override
72116 public void actionPerformed (ActionEvent e ) {
73117 JFileChooser fileDialog = new JFileChooser ();
74118 fileDialog .setFileSelectionMode (JFileChooser .FILES_ONLY );
75- fileDialog .addChoosableFileFilter (new FileNameExtensionFilter ("Scalable Vector Graphics (.svg)" , "svg" ));
76- fileDialog .addChoosableFileFilter (new FileNameExtensionFilter ("Autodesk CAD (.dxf)" , "dxf" ));
77- fileDialog .addChoosableFileFilter (new FileNameExtensionFilter ("Carbide Create (.c2d)" , "c2d" ));
78- fileDialog .addChoosableFileFilter (new FileNameExtensionFilter ("Eagle (.mnt, .mnb)" , "mnt" , "mnb" ));
79- fileDialog .addChoosableFileFilter (new FileNameExtensionFilter ("KiCad (.pos)" , "pos" ));
119+ Arrays .asList (FILE_NAME_EXTENSION_FILTERS ).forEach (fileDialog ::addChoosableFileFilter );
80120 fileDialog .showOpenDialog (SwingHelpers .getRootFrame ());
81121
82122 BackendAPI backend = CentralLookup .getDefault ().lookup (BackendAPI .class );
83123
84124 ThreadHelper .invokeLater (() -> {
85125 File f = fileDialog .getSelectedFile ();
86126 if (f != null ) {
87-
88- Optional <Design > optionalDesign = Optional .empty ();
89- if (StringUtils .endsWithIgnoreCase (f .getName (), ".svg" )) {
90- SvgReader svgReader = new SvgReader ();
91- optionalDesign = svgReader .read (f );
92- } else if (StringUtils .endsWithIgnoreCase (f .getName (), ".dxf" )) {
93- DxfReader reader = new DxfReader (backend .getSettings ());
94- optionalDesign = reader .read (f );
95- } else if (StringUtils .endsWithIgnoreCase (f .getName (), ".c2d" )) {
96- C2dReader reader = new C2dReader ();
97- optionalDesign = reader .read (f );
98- } else if (StringUtils .endsWithIgnoreCase (f .getName (), ".mnt" ) ||
99- StringUtils .endsWithIgnoreCase (f .getName (), ".mnb" )) {
100- EaglePnpReader reader = new EaglePnpReader ();
101- optionalDesign = reader .read (f );
102- } else if (StringUtils .endsWithIgnoreCase (f .getName (), ".pos" )) {
103- KiCadPosReader reader = new KiCadPosReader ();
104- optionalDesign = reader .read (f );
105- }
106-
107- if (optionalDesign .isPresent ()) {
108- Design design = optionalDesign .get ();
109- controller .setTool (Tool .SELECT );
110- controller .addEntities (design .getEntities ());
111- controller .getSelectionManager ().addSelection (design .getEntities ());
112- controller .getDrawing ().repaint ();
113- } else {
114- throw new RuntimeException ("Could not open: " + f .getName ());
115- }
127+ readDesign (controller , backend , f );
116128 }
117129 });
118130 }
0 commit comments