From e3f34ac87bc28c22168412dec5bd678c10a44c6e Mon Sep 17 00:00:00 2001 From: Chris Eade Date: Thu, 30 Apr 2020 14:22:59 +0200 Subject: [PATCH] Add --debug flag for adding debug messages Trying to track down a normals problem with studs on 3020.dat model. --- bin/dat2stl | 5 +++++ lib/LDraw/Parser.pm | 29 ++++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/bin/dat2stl b/bin/dat2stl index 2fa5443..e277f6a 100755 --- a/bin/dat2stl +++ b/bin/dat2stl @@ -14,6 +14,7 @@ GetOptions( 'scale=s', 'ldrawdir=s', 'file=s', + 'debug', ); if ( $opts->{help} ) { @@ -46,6 +47,9 @@ Takes an ldraw part .dat file as input and converts it into an STL file. non-free package "ldraw-parts" installs to /usr/share/ldraw and that is the default value for this tool. + --debug + Print debugging messages to STDERR + END } @@ -53,6 +57,7 @@ my $parser = LDraw::Parser->new( { file => $opts->{file}, $opts->{scale} ? ( scale => $opts->{scale} ) : (), $opts->{ldrawdir} ? ( ldraw_path => $opts->{ldrawdir} ) : (), + $opts->{debug} ? ( debug => 1 ) : (), } ); $parser->parse; diff --git a/lib/LDraw/Parser.pm b/lib/LDraw/Parser.pm index 9dc3aaa..7dd7332 100644 --- a/lib/LDraw/Parser.pm +++ b/lib/LDraw/Parser.pm @@ -37,10 +37,34 @@ has invert => ( documentation => 'Invert this part', ); +has debug => ( + is => 'rw', + isa => 'Bool', + default => 0, + documentation => 'Print debugging messages to stderr', +); + +has d_indent => ( + is => 'ro', + isa => 'Int', + default => 0, + documentation => 'Indentation for debug messages (for subfiles)', +); + use constant X => 0; use constant Y => 1; use constant Z => 2; +sub DEBUG { + my ( $self, $message, @args) = @_; + return if !$self->debug; + my $indent = " " x $self->d_indent; + if ( @args ) { + $message = sprintf($message, @args); + } + print STDERR sprintf("%s%s\n", $indent, $message); +} + sub parse { my ( $self ) = @_; return $self->parse_file( $self->file ); @@ -95,7 +119,6 @@ sub parse_line { sub parse_comment_or_meta { my ( $self, $rest ) = @_; - my @items = split( /\s+/, $rest ); my $first = shift @items; @@ -111,6 +134,7 @@ sub handle_bfc_command { if ( $first && $first eq 'INVERTNEXT' ) { $self->invert( 1 ); + $self->DEBUG('handle_bfc_command(): inverted model'); } } @@ -174,10 +198,13 @@ sub parse_sub_file_reference { return; } + $self->DEBUG('parse_sub_file_reference(): parsing subfile "%s" with inverted: %d', $subpart_filename, $self->invert); my $subparser = __PACKAGE__->new( { file => $subpart_filename, ldraw_path => $self->ldraw_path, invert => $self->invert, + debug => $self->debug, + d_indent => $self->d_indent + 2, } ); $subparser->parse;