| 1 | package CAD::Drawing::IO::OpenDWG; | ||
| 2 | our $VERSION = '0.22'; | ||
| 3 | |||
| 4 | #use CAD::Drawing; # circular requirements? | ||
| 5 | 1 | ▶ | use CAD::Drawing::Defined; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 0 | ▶ | use CAD::Drawing::IO::DWGI; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 7 | |||
| 8 | use strict; | ||
| 9 | use Carp; | ||
| 10 | |||
| 11 | ######################################################################## | ||
| 12 | our %filetype = ( | ||
| 13 | "dwg" => $DWG::AD_DWG, | ||
| 14 | "dxf" => $DWG::AD_DXF, | ||
| 15 | "bdxf" => $DWG::AD_BDXF | ||
| 16 | ); | ||
| 17 | |||
| 18 | our %fileversion = ( | ||
| 19 | "2000" => $DWG::AD_ACAD2000, | ||
| 20 | "14" => $DWG::AD_ACAD14, | ||
| 21 | ); | ||
| 22 | ######################################################################## | ||
| 23 | =pod | ||
| 24 | |||
| 25 | =head1 NAME | ||
| 26 | |||
| 27 | CAD::Drawing::IO::OpenDWG - Accessor methods for OpenDWG toolkit wrapper | ||
| 28 | |||
| 29 | =head1 AUTHOR | ||
| 30 | |||
| 31 | Eric L. Wilhelm <ewilhelm at cpan dot org> | ||
| 32 | |||
| 33 | http://scratchcomputing.com | ||
| 34 | |||
| 35 | =head1 COPYRIGHT | ||
| 36 | |||
| 37 | This module is copyright (C) 2004-2006 by Eric L. Wilhelm. Portions | ||
| 38 | copyright (C) 2003 by Eric L. Wilhelm and A. Zahner Co. | ||
| 39 | |||
| 40 | =head1 LICENSE | ||
| 41 | |||
| 42 | This module is distributed under the same terms as Perl. See the Perl | ||
| 43 | source package for details. | ||
| 44 | |||
| 45 | You may use this software under one of the following licenses: | ||
| 46 | |||
| 47 | (1) GNU General Public License | ||
| 48 | (found at http://www.gnu.org/copyleft/gpl.html) | ||
| 49 | (2) Artistic License | ||
| 50 | (found at http://www.perl.com/pub/language/misc/Artistic.html) | ||
| 51 | |||
| 52 | =head1 NO WARRANTY | ||
| 53 | |||
| 54 | This software is distributed with ABSOLUTELY NO WARRANTY. The author, | ||
| 55 | his former employer, and any other contributors will in no way be held | ||
| 56 | liable for any loss or damages resulting from its use. | ||
| 57 | |||
| 58 | =head1 Modifications | ||
| 59 | |||
| 60 | The source code of this module is made freely available and | ||
| 61 | distributable under the GPL or Artistic License. Modifications to and | ||
| 62 | use of this software must adhere to one of these licenses. Changes to | ||
| 63 | the code should be noted as such and this notification (as well as the | ||
| 64 | above copyright information) must remain intact on all copies of the | ||
| 65 | code. | ||
| 66 | |||
| 67 | Additionally, while the author is actively developing this code, | ||
| 68 | notification of any intended changes or extensions would be most helpful | ||
| 69 | in avoiding repeated work for all parties involved. Please contact the | ||
| 70 | author with any such development plans. | ||
| 71 | |||
| 72 | =head1 SEE ALSO | ||
| 73 | |||
| 74 | CAD::Drawing | ||
| 75 | CAD::Drawing::IO | ||
| 76 | CAD::Drawing::IO::DWGI | ||
| 77 | |||
| 78 | =cut | ||
| 79 | ######################################################################## | ||
| 80 | |||
| 81 | # the following are required to be a disc I/O plugin: | ||
| 82 | our $can_save_type = "dwg"; | ||
| 83 | our $can_load_type = $can_save_type; | ||
| 84 | our $is_inherited = 1; | ||
| 85 | |||
| 86 | =head1 Requisite Plug-in Functions | ||
| 87 | |||
| 88 | See CAD::Drawing::IO for a description of the plug-in architecture. | ||
| 89 | |||
| 90 | =head2 check_type | ||
| 91 | |||
| 92 | $type_over_ride = check_type($filename, $type); | ||
| 93 | |||
| 94 | =cut | ||
| 95 | sub check_type { | ||
| 96 | my ($filename, $type) = @_; | ||
| 97 | my ($t, $v) = dwgtype($type); | ||
| 98 | (defined($t) && defined($v)) && return($type); | ||
| 99 | # print "passed that\n"; | ||
| 100 | my $extension; | ||
| 101 | if($filename =~ m/.*\.(\w+)$/) { | ||
| 102 | $extension = $1; | ||
| 103 | } | ||
| 104 | if(defined($type)) { | ||
| 105 | $extension = $type; | ||
| 106 | } | ||
| 107 | else { | ||
| 108 | $extension ||= $type; | ||
| 109 | } | ||
| 110 | $extension = lc($extension); | ||
| 111 | my %change = ( | ||
| 112 | dwg => "dwg2000", | ||
| 113 | dxf => "dxf2000", | ||
| 114 | ); | ||
| 115 | $change{$extension} && (return($change{$extension})); | ||
| 116 | } # end subroutine check_type definition | ||
| 117 | ######################################################################## | ||
| 118 | |||
| 119 | =head1 Back-End Methods | ||
| 120 | |||
| 121 | These are called directly from CAD::Drawing::IO | ||
| 122 | |||
| 123 | =head2 load | ||
| 124 | |||
| 125 | $drw->load($filename, \%options); | ||
| 126 | |||
| 127 | =cut | ||
| 128 | sub load { | ||
| 129 | my $self = shift; | ||
| 130 | my($filename, $opt) = @_; | ||
| 131 | (-e $filename) || croak("$filename does not exist\n"); | ||
| 132 | my %opts = %$opt; | ||
| 133 | #################################################################### | ||
| 134 | $opts{verbose} && (my $verbose_load = 1); | ||
| 135 | my $dwg = CAD::Drawing::IO::DWGI->new(); | ||
| 136 | $dwg->loadfile($filename); | ||
| 137 | $dwg->getentinit(); # starts up the objecthandles | ||
| 138 | my($s, $n) = check_select($opt); | ||
| 139 | my $count = 0; | ||
| 140 | while(my($layer, $color, $type) = $dwg->getent()) { | ||
| 141 | $s->{l} && ($s->{l}{$layer} || next); | ||
| 142 | $n->{l} && ($n->{l}{$layer} && next); | ||
| 143 | # FIXME: color is only 0-256 in the world of autodesk, should | ||
| 144 | # quit using it here | ||
| 145 | $s->{c} && ($s->{c}{$color} || next); | ||
| 146 | $n->{c} && ($n->{c}{$color} && next); | ||
| 147 | $type = $dwg->entype($type); | ||
| 148 | $s->{t} && ($s->{t}{$type} || next); | ||
| 149 | $n->{t} && ($n->{t}{$type} && next); | ||
| 150 | # FIXME: What if we want to load everything into one layer? | ||
| 151 | # FIXME: must support that here. | ||
| 152 | my %pass = ( | ||
| 153 | "layer" => $layer, | ||
| 154 | "color" => $color, | ||
| 155 | ); | ||
| 156 | # here we will support reading the extrusion direction: | ||
| 157 | if(my $extrusion = $dwg->get_extrusion()) { | ||
| 158 | $pass{extrusion} = $extrusion; | ||
| 159 | # print "yippee! extrusion: ", join(",", @$extrusion), "\n"; | ||
| 160 | # see CAD::Drawing::Calculate for coordinate system handling | ||
| 161 | } | ||
| 162 | # beginning of the if`ing | ||
| 163 | my $addr; | ||
| 164 | if($type eq "lines") { | ||
| 165 | my $ld = $dwg->getLine(); | ||
| 166 | $addr = $self->addline($ld->{pts}, \%pass); | ||
| 167 | } | ||
| 168 | elsif($type eq "plines") { | ||
| 169 | my $pl = $dwg->getLWPline(); | ||
| 170 | # FIXME: someone should check the elevation eh? | ||
| 171 | $pass{closed} = $pl->{closed}; | ||
| 172 | # print "closed set to $pl->{closed}\n"; | ||
| 173 | $addr = $self->addpolygon($pl->{pts}, \%pass); | ||
| 174 | } | ||
| 175 | elsif($type eq "texts") { | ||
| 176 | my $tx = $dwg->getText(); | ||
| 177 | $pass{height} = $tx->{height}; | ||
| 178 | $tx->{angle} and ($pass{angle} = $tx->{angle}); | ||
| 179 | $addr = $self->addtext($tx->{pt}, $tx->{string}, \%pass); | ||
| 180 | } | ||
| 181 | elsif($type eq "points") { | ||
| 182 | my $pt = $dwg->getPoint(); | ||
| 183 | $addr = $self->addpoint($pt->{pt}, \%pass); | ||
| 184 | } | ||
| 185 | elsif($type eq "circles") { | ||
| 186 | my $ci = $dwg->getCircle(); | ||
| 187 | $addr = $self->addcircle($ci->{pt}, $ci->{rad}, \%pass); | ||
| 188 | } | ||
| 189 | elsif($type eq "arcs") { | ||
| 190 | my $ar = $dwg->getArc(); | ||
| 191 | $addr=$self->addarc($ar->{pt},$ar->{rad},$ar->{angs},\%pass); | ||
| 192 | } | ||
| 193 | elsif($type eq "images"){ | ||
| 194 | my $im = $dwg->getImage(); | ||
| 195 | $pass{size} = [@{$im->{size}}]; | ||
| 196 | $pass{vector} = [ [@{$im->{uvec}}], [@{$im->{vvec}}] ]; | ||
| 197 | $pass{fullpath} = $im->{fullpath}; | ||
| 198 | $pass{clipping} = $im->{clipping}; | ||
| 199 | $addr = $self->addimage($im->{pt}, \%pass); | ||
| 200 | } | ||
| 201 | else { | ||
| 202 | # warn "unknown type $type\n"; | ||
| 203 | } | ||
| 204 | |||
| 205 | if($pass{extrusion}) { | ||
| 206 | # if we made an entity in ocs, I think we should be nice | ||
| 207 | # here and put it in the wcs (but I might be insane.) | ||
| 208 | $self->to_wcs($addr); | ||
| 209 | } | ||
| 210 | $count++; | ||
| 211 | # FIXME: are we pushing the $addr to a list? | ||
| 212 | |||
| 213 | } # end while getent() | ||
| 214 | # XXX need to return all of the loaded addrs? | ||
| 215 | # sure, but at least return true for now! | ||
| 216 | return($count); | ||
| 217 | } # end subroutine load definition | ||
| 218 | ######################################################################## | ||
| 219 | |||
| 220 | =head2 save | ||
| 221 | |||
| 222 | This needs some work still. | ||
| 223 | |||
| 224 | $drw->save($filename, \%options); | ||
| 225 | |||
| 226 | =cut | ||
| 227 | sub save { | ||
| 228 | my $self = shift; | ||
| 229 | my($filename, $opts) = @_; | ||
| 230 | ##print "saving to $filename\n"; | ||
| 231 | my $type = $opts->{type}; | ||
| 232 | my($filetype, $version) = dwgtype($type); | ||
| 233 | unless(defined($filetype) && defined($version)) { | ||
| 234 | # print "trying type again\n"; | ||
| 235 | $type = check_type($filename, $type); | ||
| 236 | $type or croak("couldn't get DWG type and version for $type\n"); | ||
| 237 | ($filetype, $version) = dwgtype($type); | ||
| 238 | # print "using type $filetype and version $version\n"; | ||
| 239 | } | ||
| 240 | |||
| 241 | my $dwg = CAD::Drawing::IO::DWGI->new(); | ||
| 242 | $dwg->newfile($version); | ||
| 243 | $dwg->getentinit(); | ||
| 244 | $opts->{verbose} && print "starting dwg save\n"; | ||
| 245 | # $kok was an attempt at a speed hack which would help free the | ||
| 246 | # memory as it was being saved when working with large drawings. | ||
| 247 | # This is probably no longer needed. | ||
| 248 | my $kok = $opts->{killok}; | ||
| 249 | |||
| 250 | # Note that $dwg->writeLayer sets that layer as the current one | ||
| 251 | # until it gets called again. Therefore, we must write everything | ||
| 252 | # on "0" first and then write everything that is not "0". | ||
| 253 | # Otherwise, we would have to explicitely do a setLayer() (maybe | ||
| 254 | # that would work, but I haven't really tested it, don't care to, | ||
| 255 | # and it seems wasteful.) | ||
| 256 | |||
| 257 | # FIXME: not supporting selective saves yet! | ||
| 258 | |||
| 259 | # NOTE: this would be the most effective way to selective-save: | ||
| 260 | my $items = $self->select_addr({all => 1}); | ||
| 261 | |||
| 262 | # FIXME: how much time are we losing to this loopiness? | ||
| 263 | foreach my $item (@$items) { | ||
| 264 | $self->to_ocs($item); | ||
| 265 | } | ||
| 266 | |||
| 267 | foreach my $layer ("0", grep({$_ ne "0"} keys(%{$self->{g}}))) { | ||
| 268 | # FIXME: allow an option to flatten layers? | ||
| 269 | # FIXME: this would do colors by layer: | ||
| 270 | my %opts = ("name" =>$layer); | ||
| 271 | ($layer eq "0") || $dwg->writeLayer(\%opts); | ||
| 272 | # print "writing to $layer\n"; | ||
| 273 | foreach my $ent (keys(%{$self->{g}{$layer}}) ) { | ||
| 274 | if($ent eq "lines") { | ||
| 275 | foreach my $id (keys(%{$self->{g}{$layer}{$ent}})) { | ||
| 276 | # print "writing line\n"; | ||
| 277 | my %addr = ( | ||
| 278 | "layer" => $layer, | ||
| 279 | "type" => $ent, | ||
| 280 | "id" => $id, | ||
| 281 | ); | ||
| 282 | my $obj = $self->getobj(\%addr); | ||
| 283 | $dwg->writeLine($obj); | ||
| 284 | $kok && $self->remove(\%addr); | ||
| 285 | } | ||
| 286 | } | ||
| 287 | elsif($ent eq "plines") { | ||
| 288 | foreach my $id (keys(%{$self->{g}{$layer}{$ent}})) { | ||
| 289 | # FIXME: probably should not build our own addresses: | ||
| 290 | my %addr = ( | ||
| 291 | "layer" => $layer, | ||
| 292 | "type" => $ent, | ||
| 293 | "id" => $id, | ||
| 294 | ); | ||
| 295 | my $obj = $self->getobj(\%addr); | ||
| 296 | unless(defined($obj->{elevation})) { | ||
| 297 | my $elev_avg; | ||
| 298 | foreach my $point (@{$obj->{pts}}) { | ||
| 299 | $point->[2] || last; | ||
| 300 | # average the z-coordinates? | ||
| 301 | $elev_avg += $point->[2]; | ||
| 302 | #print "z-value: $point->[2]\n"; | ||
| 303 | } | ||
| 304 | $elev_avg /= scalar(@{$obj->{pts}}); | ||
| 305 | # print "elevation result $elev_avg\n"; | ||
| 306 | # FIXME: configurable? | ||
| 307 | if(sprintf("%0.6f", $elev_avg)) { | ||
| 308 | $obj->{elevation} = $elev_avg; | ||
| 309 | } | ||
| 310 | } | ||
| 311 | $dwg->writeLWPline($obj); | ||
| 312 | $kok && $self->remove(\%addr); | ||
| 313 | } | ||
| 314 | } | ||
| 315 | elsif($ent eq "texts") { | ||
| 316 | foreach my $id (keys(%{$self->{g}{$layer}{$ent}})) { | ||
| 317 | my %addr = ( | ||
| 318 | "layer" => $layer, | ||
| 319 | "type" => $ent, | ||
| 320 | "id" => $id, | ||
| 321 | ); | ||
| 322 | my $obj = $self->getobj(\%addr); | ||
| 323 | $dwg->writeText($obj); | ||
| 324 | # print "text string: $obj->{string}\n"; | ||
| 325 | $kok && $self->remove(\%addr); | ||
| 326 | } | ||
| 327 | } | ||
| 328 | elsif($ent eq "points") { | ||
| 329 | foreach my $id (keys(%{$self->{g}{$layer}{$ent}})) { | ||
| 330 | my %addr = ( | ||
| 331 | "layer" => $layer, | ||
| 332 | "type" => $ent, | ||
| 333 | "id" => $id, | ||
| 334 | ); | ||
| 335 | my $obj = $self->getobj(\%addr); | ||
| 336 | # print "point to toolkit: @{$obj->{pt}}\n"; | ||
| 337 | $dwg->writePoint($obj); | ||
| 338 | $kok && $self->remove(\%addr); | ||
| 339 | } | ||
| 340 | } | ||
| 341 | elsif($ent eq "circles") { | ||
| 342 | foreach my $id (keys(%{$self->{g}{$layer}{$ent}})) { | ||
| 343 | my %addr = ( | ||
| 344 | "layer" => $layer, | ||
| 345 | "type" => $ent, | ||
| 346 | "id" => $id, | ||
| 347 | ); | ||
| 348 | # FIXME: I sure do not like this: | ||
| 349 | $self->to_ocs(\%addr); | ||
| 350 | my $obj = $self->getobj(\%addr); | ||
| 351 | $dwg->writeCircle($obj); | ||
| 352 | $self->to_wcs(\%addr); | ||
| 353 | $kok && $self->remove(\%addr); | ||
| 354 | } | ||
| 355 | } | ||
| 356 | elsif($ent eq "arcs") { | ||
| 357 | foreach my $id (keys(%{$self->{g}{$layer}{$ent}})) { | ||
| 358 | my %addr = ( | ||
| 359 | "layer" => $layer, | ||
| 360 | "type" => $ent, | ||
| 361 | "id" => $id, | ||
| 362 | ); | ||
| 363 | my $obj = $self->getobj(\%addr); | ||
| 364 | $dwg->writeArc($obj); | ||
| 365 | $kok && $self->remove(\%addr); | ||
| 366 | } | ||
| 367 | } | ||
| 368 | } # end foreach $ent | ||
| 369 | } # end foreach $layer | ||
| 370 | |||
| 371 | # FIXME: how much time are we losing to this loopiness? | ||
| 372 | unless($kok) { | ||
| 373 | foreach my $item (@$items) { | ||
| 374 | $self->to_wcs($item); | ||
| 375 | } | ||
| 376 | } | ||
| 377 | ## print "saving to $filename\n"; | ||
| 378 | my $res = $dwg->savefile($filename, $filetype); | ||
| 379 | $opts->{verbose} && print "finished save\n"; | ||
| 380 | return($res); | ||
| 381 | } # end subroutine save definition | ||
| 382 | ######################################################################## | ||
| 383 | |||
| 384 | =head1 Internal Methods | ||
| 385 | |||
| 386 | The back-end methods should definitely not be called directly, and these | ||
| 387 | should not even be called from the backend code. | ||
| 388 | |||
| 389 | =cut | ||
| 390 | ######################################################################## | ||
| 391 | |||
| 392 | =head2 dwgtype | ||
| 393 | |||
| 394 | Returns the toolkit constants corresponding to some human-readable | ||
| 395 | version and type names. | ||
| 396 | |||
| 397 | ($type, $version) = dwgtype($type); | ||
| 398 | |||
| 399 | =cut | ||
| 400 | sub dwgtype { | ||
| 401 | my($type) = @_; | ||
| 402 | my %filetype = ( | ||
| 403 | "dwg" => 0, # AD_DWG | ||
| 404 | "dxf" => 1, # AD_DXF | ||
| 405 | "bdxf" => 2, # AD_BDXF | ||
| 406 | ); | ||
| 407 | my %fileversion = ( | ||
| 408 | "2000" => 7, #AD_ACAD2000 | ||
| 409 | "14" => 6, #AD_ACAD14 | ||
| 410 | "13" => 5, | ||
| 411 | "11" => 4, | ||
| 412 | "10" => 3, | ||
| 413 | "9" => 2, | ||
| 414 | "26" => 1, | ||
| 415 | "25" => 0 | ||
| 416 | ); | ||
| 417 | if($type =~ m/^((?:dwg)|(?:b?dxf))(\d+)$/) { | ||
| 418 | my $typespec = $1; | ||
| 419 | my $version = $2; | ||
| 420 | unless(defined($filetype{$typespec})) { | ||
| 421 | carp("no type for $typespec\n"); | ||
| 422 | } | ||
| 423 | unless(defined($fileversion{$version})) { | ||
| 424 | carp("no version for $version\n"); | ||
| 425 | } | ||
| 426 | return($filetype{$typespec}, $fileversion{$version}); | ||
| 427 | } | ||
| 428 | return(); | ||
| 429 | } # end subroutine dwgtype definition | ||
| 430 | ######################################################################## | ||